Video: Entity Framework 5 Enums and Moving Solution from EF 4.3

I moved a small EF 4.3 solution to EF5 (and to .NET 4.5) so I can add the new enum support to it. Then I started the process all over again and captured it as a screencast.

Pluralsight is hosting it on their blog here: Video: Entity Framework 5 New Features Sneak Peek. It’s 14 minutes long. I learned a whole bunch of tricks along with way which I’ve shared here.

  • Moving a solution from EF4.3 +.NET 4 to EF5 +.NET 4.5
  • Using the new SQL Server Object Explorer in Visual Studio 11
  • Working with the new DbLocal database
  • Seeing the enum support in action : how it impacts the database, inserting and querying data with enums.

  Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!  

20 thoughts on “Video: Entity Framework 5 Enums and Moving Solution from EF 4.3

  1. Hi Juile,

    I always enjoy your videos, thanks.

    One follow up question:

    What happens when enums values are reordered or new values added?

  2. Thanks for putting this video together.

    Did you happen to test out bitwise enums. I’m curious if you’re able to query a hasflag type scenario.

    Thanks

  3. Hi Nathan,

    I did not but this from the team blog post should start to dispel any concerns: "Enumerations that can represent multiple values simultaneously are a powerful concept, but the implementation in .NET is extremely simple: the programing languages handle all enum types in the same way, e.g. bitwise operators can be applied to enums regardless of the presence of FlagsAttribute (in fact, the attribute’s sole impact in the behavior of the Base Class Library is in the conversion of an enum value to a string). In other words, there is no strong contract in using FlagsAttribute, and it is up to the developer to use only enums that were meant for multi-value in such a way."

    (http://blogs.msdn.com/b/efdesign/archive/2011/06/29/enumeration-support-in-entity-framework.aspx)

  4. Thanks Julie. I threw together a quick test and it looks like it’s not supported in beta 2.

    This query:

    db.Users.Where(d => d.Roles.HasFlag(Roles.Administrator)).ToList()

    Results in this error:

    LINQ to Entities does not recognize the method ‘Boolean HasFlag(System.Enum)’ method, and this method cannot be translated into a store expression.

    It handles the storage just fine, just not the query.

    Nathan

  5. Hi Julie,

    Greetings from just up the road a piece in the 05403. Thanks for giving this preview! I was wondering if you’d tested any char value to enum mappings. For instance, in a legacy DB we have rule governed char values in a column that would be a natural fit for mapping to enums in the CLR. Along those same lines, we have char(1) vals of Y/N that should map to a CLR bool. Just wondering if you’ve seen anything built in to help with that or failing that, a way to inject custom code into the mapping pipeline to handle it ourselves (NHibernate style). Thanks in advance!

    -Chris

  6. Julie,

    I think it’s so cool to see women in the Software area! 😀

    I’m from Brazil and just want to say that you speak very clearly/fluently. I understood each and every word you spoke.

    I also shared this awesome video on an old question at StackOverflow to help spread the word about the awesomeness that comes with Entity Framework. stackoverflow.com/…/114029

    Keep these great videos coming!

    All the best,

    Leniel

  7. Julie,

    Great video! Do you have an example of the html code you would use in your view to use a drop down listbox for the enum support in an edit view or displaying the value in a view.

    Thanks

    Bruce

  8. Great video Julie. I would love to see a follow up video on flags and how they are stored etc.

    Keep up the fantastic work — now if I could get everyone to spoonfeed me answers.

  9. Nice tutorial on EF, i got your blog from plural sight video tutorial and i found your blog it very helpful for me.

    I hope’ it ‘ll keep me updated with latest updates of EF… Thanks for providing good stuff ..

  10. Hey its been some time, please post a new video on plural sight.

    I am missing EF5 video on pluralsight 🙂

  11. Hi Julie

    Apologies if this is not the correct place for this ‘comment’ but I’m hoping that you might be able to fix a problem that I’m having with EF…I’m only asking you as a last resort as I cannot find anywhere on the internet that deals directly with this subject (and I appreciate that you are one of the leading experts in this field).

    Using EF 4 Model First, I’ve created a database and a class library from the model.

    In a legacy application, I’ve then added a reference to this class library and amended the legacy classes so that they inherit from the ‘model’ classes.

    For example:

    public class LegacyClassParams : Model.Param

    All works well until I try to write the legacy class object to the database. Needless to say, I thought that one could simply upcast the derived class to the model base class – but No!

    However I try this (record = legacyParams as Model.Param, tried with DbSet, etc) nothing seems to work with EF always refusing (no table mapping for derived class, etc).

    It seems as if the Model base object type changes to the derived class’s object type whenever it is upcast.

    Any pointers would be most appreciated and please forgive this intrusion (and my obvious lack of knowledge).

    with kind regards,

    Derek JW Cahusac de Caux

  12. Hi Julie,

    I am developing a Silverlight 5/EF 5 application in a two tier project using RIA services against SQL server 2005. The application is in an Inquiry based system, and 95% of the calls are stored procedures. I have several domain services representing the different business areas involved. I do not need to send data back to the database. I am using DBContext. Should I be dropping down to ObjectContext for better performance?

    Also, if I just stick with DBContext, what is DbContext.Configuration.AutoDetectChangesEnabled = False

    Thanks

  13. Hi Julie,

    I am developing a Silverlight 5/EF 5 application in a two tier project using RIA services against SQL server 2005. The application is in an Inquiry based system, and 95% of the calls are stored procedures. I have several domain services representing the different business areas involved. I do not need to send data back to the database. I am using DBContext. Should I be dropping down to ObjectContext for better performance?

    Also, if I just stick with DBContext, what is DbContext.Configuration.AutoDetectChangesEnabled = False

    Thanks

    .

  14. I don’t know very much about Silverlight or RIA Services. So with them involved I can’t say how perf is affected. According to ef team post on ef5 performance, there is some benefit to querying via objectcontext over dbcontext. Not sure where it’s coming from. You should use NoTracking queries for data that doesn’t need to be modified/updated. Sorry I’m not providing links for each of these things but they are all easily found via googlebing. hth

  15. Julie, it was great pleasure watching your video.

    Few thoughts: After the database is created by EF5, If somebody else plans to use the database he/she would never know what those 1,2,3 means. I know this is a Microsoft way of doing stuff, but I was under the impression that enum would create a foreign key lookup table in SQL Server and create a join to a main table. Or in Opposite way, if I want to create create entity model using databse first design, I should be able to treat foreign key lookup table values as an enum.

    Thanks, You helped me understand EF.

Leave a Reply to Leniel Macaferi Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.