Question

Got an issue where i've updated an application from EF4.1 to 6.0.2 to take advantage of Migrations going forward. But since upgrading automapper now fails to convert an id to a enum I use the following code and its work for a long time.

Mapper.CreateMap<Payment, ListTowDto>()
    .ForMember(x => x.PaymentStatus, 
        opt => opt.MapFrom(s => Enum.GetName(typeof(PaymentStatus), s.PaymentStatusId)))

Is there anything new to be aware of ?

Also I've upgraded AutoMapper to 3.1.1 (was using autmapper 2.? and ef 4.1)

Edit:

Im getting the following error:

Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'PaymentStatus'

And the dto object being converted to has the following within it:

public int ApprovalStatusId { get; set; }    
public virtual ApprovalStatus ApprovalStatus { get; set; }

Cheers

Was it helpful?

Solution

Ok I worked it out.

For those that upgrade from EF4 to EF6 and used Enum virtual references in there Entity model for auto populating with AutoMapper or the like, You need to annotate the virtual fields with [NotMapped].

So in my case the following will give same functionality as before Enum support was introduced with Entity Framework 5

public int ApprovalStatusId { get; set; } 
[NotMapped]   
public virtual ApprovalStatus ApprovalStatus { get; set; }

For those looking how to declare an Enum in an EF5+ entity model, you need to just declare the enum in the model so if i was to start this model from scratch in future i would remove my int PaymentStatusId from my model, and also remove the virtual keyword from ApprovalStatus as it becomes useless in this situation.

public ApprovalStatus ApprovalStatus { get; set; }

The database will store the int value of the enum to the datastore column of ApprovalStatus.

Cheers all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top