将 ADO.net 实体框架 4 与枚举一起使用?我该怎么做?
问题 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然
int <->枚举
赢了' EF4 的初始版本不支持。我同意那些说这很糟糕的人的观点。我正在使用一个为我进行转换的属性,
如果您“正确”地命名内容,那么看起来并没有那么糟糕(这意味着,所以它看起来并不愚蠢)。例如,我有一个 ReasonCode 枚举,它作为 Reason 存储在数据库中,因此我有该属性的 Reason 和 ReasonCode 版本。目前效果还不错。
首先,我刚刚开始使用 EF4,所以我不太了解它的工作原理。我认为它与 L2S 类似,但具有更好的实体支持。请对此持保留态度。
需要明确的是,此属性是为了方便起见,我 90% 确信如果您尝试使用此属性查询数据库,EF 会做出很糟糕的反应。 EF 不知道您的属性,因此无法使用它来构造 sql。因此, this:
可能无法按预期运行,而 this:
可能会导致整个 Foos 表被带入内存,然后进行解析。 这个属性是为了方便,只有在数据库被命中之后才应该使用! 如:
如果你不明白这里的区别,那么你就是在玩火。您需要了解 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
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:
will probably fail to behave as expected where this:
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:
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.