如何更换一个int属性与实体框架枚举? [英] How do I replace an Int property with an Enum in Entity Framework?

查看:156
本文介绍了如何更换一个int属性与实体框架枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与数据类型为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?

推荐答案

间接地,像<一个href="http://weblogs.asp.net/alexeyzakharov/archive/2008/09/18/enum-in-ado-net-entity-framework-v1.aspx"相对=nofollow>所以。

就个人而言,我离开存储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.

这篇关于如何更换一个int属性与实体框架枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆