使用ADO.net实体框架4与枚举?我该怎么做? [英] Using ADO.net Entity Framework 4 with Enumerations? How do I do it?

查看:239
本文介绍了使用ADO.net实体框架4与枚举?我该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题1:我正在玩EF4,我有一个模型类,如:

  public class Candidate {

public int Id {get; set;}
public string FullName {get; set;}
public性别{get; set;}
public EducationLevel HighestDegreeType {get ; set;}
}

这里Gender和EducationLevel是枚举,如:

  public enum Gender {男,女,未公开} 
public enum EducationLevel {HighSchool,Bachelors,Masters,Doctorate}

如果以下情况,我如何获得候选类和性别与EducationLevel与EF4合作:




  • 我首先开发模式

  • 我做db首先开发



编辑:将与对象上下文相关的问题移动到另一个问题

显然 int< - >枚举 在EF的初始版本中将不被支持 4。我同意那些说这个话的人。



我正在使用为我投放的财产

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

如果您正确地命名东西,这看起来不太好(这意味着,所以它看起来不笨)。例如,我有一个ReasonCode枚举存储在数据库中的原因,所以我有Reason和ReasonCode属性的版本。






首先,我刚刚开始使用EF4,所以我' m不亲密如何工作。我认为它类似于L2S,但具有更好的实体支持。为了方便起见,这个财产是为了方便起见,我有90%的确定,如果你尝试,EF将会发生很大的反应。



使用此属性查询数据库。 EF不知道你的属性,不能用它构造sql。所以这样:

  var foo = from D in Db.Foos其中x.HurrEnum == Hurr.Durr select x; 

可能会失败,如下所示:

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

可能会导致整个Foos表被带入内存,然后解析。 此属性是为了方便起见,只有在数据库被击中后才能使用!如:

  //这是执行在sql server 
var foo = Db.Foos.Where(x => x.Hurr == 1 || x.Hurr == 2).ToArray();
//这是在内存中完成
var hurrFoos = foo.Where(x => x.HurrEnum == Hurr.Durr);

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


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.

解决方案

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.

这篇关于使用ADO.net实体框架4与枚举?我该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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