Я нашел такой способ сделать это, и он работает, просто это не Enum напрямую.Вы должны создать класс фасада, который можно неявно отображать взад и вперед.Это работает, но не так гладко, как хотелось бы.
http://daniel.wertheim.se/2010/06/09/dealing-with-enumerations-and-entity-framework-4-code-first/
public enum InterestTypes
{
[Description("Attraction - for sightseers and adventurers.")] Attraction = 1,
[Description("Event - fun for everyone (limited time).")] Event = 2,
[Description("Recreation - parks, hiking, movies...")] Recreation = 3,
[Description("Restaurant - good eats.")] Restaurant = 4
}
public class InterestType
{
private InterestType()
: this(default(InterestTypes))
{
}
public InterestType(int value)
{
Value = value;
}
public InterestType(InterestTypes type)
{
Value = (int) type;
}
public int Value { get; private set; }
public static implicit operator int(InterestType type)
{
return type.Value;
}
public static implicit operator InterestType(int value)
{
return new InterestType(value);
}
public static implicit operator InterestTypes(InterestType type)
{
return (InterestTypes) type.Value;
}
public static implicit operator InterestType(InterestTypes type)
{
return new InterestType(type);
}
}
В вашем DataContext вам необходимо следующее в вашем методе OnModelCreating.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.ComplexType<InterestType>()
.Property(o => o.Value)
.HasColumnName("Type");
}
Это говорит Entity Framework для сопоставления значения int со значением в базе данных.Я думаю, вы также можете использовать строки, если хотите сохранить значения в виде строк в базе данных.