8

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.

1 Answer 1

16

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.

Sign up to request clarification or add additional context in comments.

10 Comments

BTW, the link is to an msdn forum question that's answered by Daniel Simmons who is dev lead on EF4.
Does this allow you to use this in a query? .Where(x => x.HurrEnum = MyEnum.Foo);?
Sure, you can do that. However, that query won't (90% sure) be converted into sql. My hack is for convenience only. If you want the where clause to make it into sql you'll have to use the int. Otherwise, it may throw or the where clause will be executed in memory rather than in sql.
Thanks for that, from what I know of EF is that it'll always throw, I haven't seen it executing part of a query in memory because it couldn't be mapped to SQL
This sucks! I hope it makes it in the .net 4 release. It doesnt make sense to say "we support POCO" without being able to use Enums, even tuples should work out of the box in some cases as part of your POCO. LAME!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.