Sco*_*合理论 5 c# orm entity-framework entity-framework-4.1
我有一个Operation像这样的枚举类型:
[Flags]
public enum Operation
{
None = 0,
View = (1 << 0),
Add = (1 << 1),
Edit = (1 << 2),
Delete = (1 << 3),
ReservedA = (1 << 4),
ReservedB = (1 << 5),
Admin = (View | Add | Edit | Delete)
}
Run Code Online (Sandbox Code Playgroud)
和表permission包含一operations列。该表映射到这样的类:
[Table("rolepermission")]
public class Permission : IComparable
{
private int _id = 0;
private Operation _operations = Operation.None;
private List<UserRole> _roles = null;
public Permission()
{
_roles = new List<UserRole>();
}
[Key]
public int Id
{
get { return _id; }
set { _id = value; }
}
[EnumDataType(typeof(Operation))]
public Operation Operations
{
get { return _operations; }
set { _operations = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用。
有什么优雅的方法可以映射它吗?
我认为创建int-type属性不是最好的方法。
.NET 4.0中的唯一方法是使用将int属性持久化到数据库和未映射的enum属性,这些属性将在内部(或在实体加载和实体持久性期间)转换int。或者,您可以尝试使用包装方法。
.NET 4.5直接支持枚举,但我认为不支持Flags(编辑:检查Kamyar的评论)。