将 ADO.net 实体框架 4 与枚举一起使用?我该怎么做?

发布于 2024-08-17 20:16:08 字数 700 浏览 3 评论 0原文

问题 1:我正在使用 EF4,我有一个模型类,如下所示:

public class Candidate {

public int Id {get;set;}
public string FullName {get;set;}
public Gender Sex {get;set;}
public EducationLevel HighestDegreeType {get;set;}
}

这里 Gender 和 EducationLevel 是枚举,例如:

public enum Gender {Male,Female,Undisclosed}
public enum EducationLevel {HighSchool,Bachelors,Masters,Doctorate}

如何让 Candidate 类以及 Gender 和 EducationLevel 与 EF4 一起使用,如果:

  • 我首先进行模型开发
  • 我首先进行数据库开发

编辑:将与对象上下文相关的问题移至另一个问题 此处

Question 1: I am playing around with EF4 and I have a model class like :

public class Candidate {

public int Id {get;set;}
public string FullName {get;set;}
public Gender Sex {get;set;}
public EducationLevel HighestDegreeType {get;set;}
}

Here Gender and EducationLevel are Enums like:

public enum Gender {Male,Female,Undisclosed}
public enum EducationLevel {HighSchool,Bachelors,Masters,Doctorate}

How do I get the Candidate Class and Gender and EducationLevel working with EF4 if:

  • I do model first development
  • I do db first development

Edit: Moved question related to object context to another question here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

[旋木] 2024-08-24 20:16:08

显然 int <->枚举 赢了' EF4 的初始版本不支持。我同意那些说这很糟糕的人的观点。

我正在使用一个为我进行转换的属性,

public partial class MyEntity
{
  public MyEnum HurrEnum {get{return (MyEnum)Hurr;} set{Hurr = (int)value;}}
}

如果您“正确”地命名内容,那么看起来并没有那么糟糕(这意味着,所以它看起来并不愚蠢)。例如,我有一个 ReasonCode 枚举,它作为 Reason 存储在数据库中,因此我有该属性的 Reason 和 ReasonCode 版本。目前效果还不错。


首先,我刚刚开始使用 EF4,所以我不太了解它的工作原理。我认为它与 L2S 类似,但具有更好的实体支持。请对此持保留态度。

需要明确的是,此属性是为了方便起见,我 90% 确信如果您尝试使用此属性查询数据库,EF 会做出很糟糕的反应。 EF 不知道您的属性,因此无法使用它来构造 sql。因此, this:

var foo = from x in Db.Foos where x.HurrEnum == Hurr.Durr select x;

可能无法按预期运行,而 this:

var foo = Db.Foos.Where(x=> x.HurrEnum == Hurr.Durr);

可能会导致整个 Foos 表被带入内存,然后进行解析。 这个属性是为了方便,只有在数据库被命中之后才应该使用! 如:

 // this is executed in the sql server
 var foo = Db.Foos.Where(x=> x.Hurr == 1 || x.Hurr == 2).ToArray();
 // this is then done in memory
 var hurrFoos = foo.Where(x=> x.HurrEnum == Hurr.Durr);

如果你不明白这里的区别,那么你就是在玩火。您需要了解 EF/L2S 如何解释您的代码并将其转换为 sql 语句。

Apparently int <-> enum won't be supported in the initial release of EF4. I agree with those who say this sucks.

I am using a property that does the casting for me

public partial class MyEntity
{
  public MyEnum HurrEnum {get{return (MyEnum)Hurr;} set{Hurr = (int)value;}}
}

It doesn't look so bad if you name stuff "correctly" (which means, so it doesn't look stupid). For instance, I have a ReasonCode enum that's stored as a Reason in the database, so I have Reason and ReasonCode versions of the property. Works out well enough, for now.


First, I'm just starting to use EF4 so I'm not intimate with how it works. I assume its similar to L2S but with better entity support. Take this with a grain of salt.

To be clear, this property is for convenience and I'm 90% sure EF will react badly if you try to query the database using this property. EF doesn't know about your property and cannot use it to construct sql. So this:

var foo = from x in Db.Foos where x.HurrEnum == Hurr.Durr select x;

will probably fail to behave as expected where this:

var foo = Db.Foos.Where(x=> x.HurrEnum == Hurr.Durr);

probably will result in the entire Foos table being brought into memory and then parsed. This property is for convenience and should be used only after the database has been hit! Such as:

 // this is executed in the sql server
 var foo = Db.Foos.Where(x=> x.Hurr == 1 || x.Hurr == 2).ToArray();
 // this is then done in memory
 var hurrFoos = foo.Where(x=> x.HurrEnum == Hurr.Durr);

If you don't understand the difference is here then you're playing with fire. You need to learn how EF/L2S interprets your code and converts it into sql statements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文