When faced with a lack of support for enum properties in Entity Framework 4, I found this article that describes a workaround:
Entity Framework 4.0 Beta 1 - POCO Enum Support?
How to express the mapping of a database column to OrderStatusWrapper (as described in the article) in the code, and not in the designer?
Update:
Based on the answers I did not understand, I need to replace the enum OrderStatus type OrderStatusWrapper , i.e.
Instead:
public class Order { public OrderStatus Status { get; set; } }
I have to use:
public class Order { public OrderStatusWrapper Status { get; set; } }
This is a bit more to me, but after executing the following code:
// OrderContext class defined elsewhere as: // public class OrderContext : DbContext // { // public DbSet<Order> Orders { get; set; } // } using(OrderContext ctx = new OrderContext()) { Order order = new Order { Status = OrderStatus.Created; } ctx.Orders.Add(order); ctx.SaveChanges(); }
The following exception (abbreviated for brevity):
System.Data.SqlClient.SqlException
Message = Invalid column name 'Value'.
The database column is called Status . I tried decorating the Status property:
[Column("Status")]
then
[Column("Status"), TypeName("OrderStatus")]
and
[Column("Status"), TypeName("OrderStatusWrapper")]
But this does not eliminate this exception.
I also tried removing the Column attribute and doing this:
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Order>() .Property(p => p.OrderStatus) .HasColumnName("OrderStatus"); }
But I get the following compilation error:
Error 1 The type "ConsoleApplication1.OrderStatusWrapper" must be an unimaginable value type in order to use it as the "T" parameter in the general type or method "System.Data.Entity.ModelConfiguration.Configuration.Types.StructuralTypeConfiguration.Property (System.Linq.Expressions. Expression>) '[disabled path]