0

I'm experimenting with using enumeration classes (link) instead and enums and "lookup tables".

I have this scenario which displays a simple list in my view where I want to display the TaskStatus name from the enumeration class instead of the StatusId but I get this error "InvalidOperationException: The entity type 'TaskStatus' requires a primary key to be defined."

Is my approach all wrong?

<table clss="table">
    @foreach(var item in Model)
    {
        <tr>
            <td>@item.Id</td>
            <td>@item.Name</td>
            <td>@item.Status</td>
        </tr>
    }
</table>

public class Tasks : BaseEntity
{
    public string Name { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? DueDate { get; set; }
    public byte StatusId { get; set; }
    public string AssignedTo { get; set; }

    public virtual TaskStatus Status { get; set; }
}

public class IndexVm
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? DueDate { get; set; }
    public byte StatusId { get; set; }

    public TaskStatus Status { get; set; } 
}

public class TaskStatus : Enumeration<TaskStatus, int>
{
    public static readonly TaskStatus NotStarted           = new TaskStatus(1, "Not Started");
    public static readonly TaskStatus InProgress           = new TaskStatus(2, "In Progress");
    public static readonly TaskStatus Completed            = new TaskStatus(3, "Completed");
    public static readonly TaskStatus WaitingOnSomeoneElse = new TaskStatus(4, "Waiting on someone else");
    public static readonly TaskStatus Deferred             = new TaskStatus(5, "Deferred");

private TaskStatus(int value, string displayName) : base(value, displayName) { }
}

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<Tasks, IndexVm>()
            .ForMember(vm => vm.Status, conf => conf.MapFrom(ol => ol.Status.DisplayName));
    }
}
Tim
  • 81
  • 3
  • 11

1 Answers1

0

Your issue has nothing to do with Automapper. The error message you are getting is from EntityFramework. Because you define a Status Property in your Entity, EF is attempting to treat the TaskStatus class as another entity and it cannot since it does not have a Primary key as the error message states.

If you're dead set on using Enum classes take a look here under the heading "Encapsulated primitives (aka the NodaTime/Enumeration class problem)"

Try changing you code to something like this:

public class IndexVm
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? DueDate { get; set; }
    public byte StatusId { get; set; }

    [NotMapped]
    public TaskStatus Status { 
        get { //Return the value of StatusVal converted to an Enumeration 
        set { //Set the value of StatusVal after converting from an Enumeration } 
    } 
    public int StatusVal { get; set; }
}
chambo
  • 491
  • 3
  • 8