如何更换一个int属性与实体框架枚举?实体、框架、属性、int

2023-09-03 10:13:17 作者:与我谈笑风生

我有一个与数据类型为int的底层数据库列的性质的实体类,但在现实中,我想这个属性是一个枚举。有没有什么办法可以指定该属性返回一个枚举?

I have an entity class that has a property with an underlying db column of datatype Int, however in reality I want this property to be an Enum. Is there any way to specify that this property returns an Enum?

推荐答案

间接地,像所以。

就个人而言,我离开存储INT公众(例如,如 DbFoo ,其中枚举属性是) - 这样我还可以写对lambda表达式在数据库执行的列,例如:

Personally, I leave the storage int public (for example as DbFoo, where the enum property is Foo) - that way I can still write lambdas against the column for execution at the DB, for example:

where row.DbFoo == SomeConstant

如果你不公开的存储值,你不能这样做的干净。你同样可以把它的内部,并在上下文中的一些方法做过滤...这里有一个我写了今天早些时候:

If you don't expose the storage value, you can't do this as cleanly. You could equally leave it internal, and have some methods in the context to do the filtering... here's one I wrote earlier today:

public IOrderedQueryable<User> Administrators
{
    get { return Users.Where(x => x.DbUserType == User.UserTypeAdmin)
             .OrderBy(x => x.Name);
}

其中, User.UserTypeAdmin 是我的内部常数。在这种情况下,我无法用一个可识别的子类,因为它干扰了ADO.NET数据服务。

where User.UserTypeAdmin is my internal constant. In this case, I couldn't use a discriminated subclass, as it was interfering with ADO.NET Data Services.