请问,如何先在Entity Framework代码中使用枚举。我想,在我的课“Annonce”我能有这个proprety首先在EF代码中枚举
public Status EtatAnnonce { get; set; }
和状态的定义如下
public enum Status
{
Pending,
Approved
}
请问,如何先在Entity Framework代码中使用枚举。我想,在我的课“Annonce”我能有这个proprety首先在EF代码中枚举
public Status EtatAnnonce { get; set; }
和状态的定义如下
public enum Status
{
Pending,
Approved
}
您需要创建一个转换器字段的值存储为int类型的数据库。
public int MyEnumValueInt {get;set;}
[NotMapped]
public MyEnum MyEnumValue
{
get{ return (MyEnum)MyEnumValueInt;
set{ MyEnumValueInt = (int)value;
}
注:支持ENUM将在EF 5
我已经回答了关于EF枚举两个问题得到改善;这些应该可以帮助您一起:
Enums with EF code-first - standard method to seeding DB and then using?
和
您可以在模型中使用的专用属性将数据映射到任何你想要的属性类型。
// Model
public class Piece
{
// Subclass Piece to add mappings for private properties
public class PieceConfig : EntityTypeConfiguration<Piece>
{
public PieceConfig()
{
Property(b => b.dbtype); // needed for EF to see the private property
}
}
[Column("type", TypeName = "VARCHAR")]
private string dbtype { get; set; }
[NotMapped]
public PIECE type
{
get { return (PIECE)Enum.Parse(typeof(PIECE), dbtype); }
set { dbtype= value.ToString(); }
}
}
然后你只需要配置添加到您的OnModelCreating方法
modelBuilder.Configurations.Add(new Piece.PieceConfig());
会为您指出
EF5 does not create enum columns
举的枚举支持实体框架代码摘要第一个:
EF4:不支持orted
EF5:如果你是针对.NET框架4.5和更高
EF6仅支持:只有当你面向.NET 4.0和更高
干杯支持!
选中此项, http://the-semicolon.blogspot.com/p/handling-enum-in-code-first-entity.html – iJay 2014-08-26 10:12:54