[英]ADO.Net - Map table char column to enum
在现有的SQL表中, nvarchar
列包含枚举信息。
我现在尝试使用一个枚举类来使用ADO.Net Entity Framework映射此列。 但是这似乎不可能,因为它需要整数列。
ADO EF是否不支持映射到char列的枚举,或者如何实现允许在查询时使用linq或lambda语法的枚举?
错误: Given element assignment is invalid.
我更习惯于在可能的情况下使orm休眠。
您可以在模型中使用私有属性,以将数据映射到所需的任何属性类型。
// 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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.