Use entity-framework to map int column for enum property

I have an enum type called Operation as follows:

 [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) } 

and the permission table contains the operations column. This table maps to this class:

 [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; } } } 

but that will not work.

Is there any elegant way that can display it?

I think creating an int -type property is not the best way.

+4
source share
1 answer

The only way in .NET 4.0 is to use the int property stored for the database and the non-displayed enum property, which will convert the int inside (or while loading the object and saving the object). Alternatively, you can try the wrapper method .

.NET 4.5 supports enums directly , but I don't think flags are supported (Edit: Check Kamyar comment).

+4
source

All Articles