none
Nested Code-first Enum's not working in EF5 + .NET4.5 + VS11 RRS feed

  • Question

  • Hi, I've recently installed VS11 RC to allow me to start playing around with the new features in EF5 RC, but for some annoying reason I cannot get Enums working using the following basic console app:

    using System.Data.Entity.ModelConfiguration;
    using System.Data.Entity;
    
    namespace EnumTest
    {
        public class Car
        {
            public enum CarMake
            {
                Ferrari,
                Ford
            }
    
            public int Id { get; set; }
            public CarMake Make { get; set; }
        }
    
        public class MyContext : DbContext
        {
            public DbSet<Car> Cars { get; set; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                MyContext context = new MyContext();
                context.Cars.Add(new Car());
                context.SaveChanges();
            }
        }
    }

    When i run the above it generates a new db in my local SQL Express server containing a 'Car' table, but it only has an 'Id' column; the 'Make' column is not generated.

    This is a VS11 RC console app, targetting .NET Framework 4.5 and I've installed the EF5 RC package using the nuget command 'install-packages entityframework -pre'. I've double checked to ensure the EntityFramework.dll reference is V5.0.0.0, so I'm at a loss as to why the enums are not working given I've seemingly set everything up correctly. It's almost as though EF5 thinks its running on .NET4.0 and therefore disables enum support.

    Am I missing something stupid?

    PS: Im running on 64-bit Win7 Ultimate and previously had VS2010 + .NET4 installed prior to upgrading to VS11 RC + .NET4.5.


    Edit:

    I just installed VS11 onto another pc and the same problem occurs, so that rules out any isolated problem on my dev pc.


    • Edited by Shane Hancock Thursday, June 7, 2012 9:12 PM Changed title to be more descriptive
    Thursday, June 7, 2012 3:01 AM

Answers

  • Hello Shane,

    Unfortunately the current version of EF doesn't support nested types. If you move the definition of CarMake out of the Car class and into the EnumTest namespace it will work as in the walkthrough.

    This is a limiation that we are looking into removing in a future version of EF.

    Thanks,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.

    • Marked as answer by Shane Hancock Thursday, June 7, 2012 9:03 PM
    Thursday, June 7, 2012 8:44 PM
    Moderator

All replies

  • Ok, i've been messing about trying to get this working all day now and still no joy, which seems kinda strange given that this blog post discusses how it now works.

    Can anyone confirm that enums do actually work in Visual Studio 11/2012 + EF5 + .NET4.5?



    Thursday, June 7, 2012 7:39 AM
  • Hi Shane

    according to this it should work, if you installed an EF 5 beta1 package or newest.

    Hope it helps!


    Thursday, June 7, 2012 7:41 AM
  • Hi Shane

    according to this it should work, if you installed an EF 5 beta1 package or newest.

    Hope it helps!


    Yeah, I've followed that blog post to the letter with VS2012 + EF5 RC + .NET4.5 on two different pc's and unfortunately still no joy.

    If someone with nothing better to do could confirm whether it *actually* works or not by following the walkthrough that'd be great. At least then I'll know if it's me doing something idiotic or not.



    Thursday, June 7, 2012 7:58 AM
  • Hi Shane,

    some time ago (still with VS11) i've tested the walkthrough following the model first approach and it worked..

    Thursday, June 7, 2012 9:00 AM
  • Hi Shane,

    some time ago (still with VS11) i've tested the walkthrough following the model first approach and it worked..

    Ok, so the Model first approach works, thats good. How about Code-first, which is what I'm struggling with?
    Thursday, June 7, 2012 12:28 PM
  • I never tried the Code First approach, sorry but i think it should work as other approaches works
    Thursday, June 7, 2012 12:56 PM
  • I never tried the Code First approach, sorry but i think it should work as other approaches works

    Unfortunately, it doesn't.

    Can someone from the ADO.Net team that wrote the above mentioned walkthrough please confirm this in code-first? I noticed that some of the comments at the end of this enum walkthrough of the June CTP release mention the same problem, so I'm guessing the same problem may have made its way into EF5 RC also.

    Thursday, June 7, 2012 8:31 PM
  • Hello Shane,

    Unfortunately the current version of EF doesn't support nested types. If you move the definition of CarMake out of the Car class and into the EnumTest namespace it will work as in the walkthrough.

    This is a limiation that we are looking into removing in a future version of EF.

    Thanks,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.

    • Marked as answer by Shane Hancock Thursday, June 7, 2012 9:03 PM
    Thursday, June 7, 2012 8:44 PM
    Moderator
  • Ahhhhh ok. Can't believe I didn't try this.

    I guess I didn't follow the walkthough to the letter like i said above after all - what a complete twat i am.

    Thanks for your help everyone.

    Thursday, June 7, 2012 9:03 PM
  • Ahhhhh ok. Can't believe I didn't try this.

    I guess I didn't follow the walkthough to the letter like i said above after all - what a complete twat i am.

    Thanks for your help everyone.

    Hello,

    I have the same problem as you. What exactly caused it not to generate the enum tables?

    The only difference in my setup is that my enums reside in a different project/namespace (is this a problem), but are referenced by my entities.

    thanks

    Friday, October 19, 2012 2:05 PM
  • Can you post a repro? Do you have entities in multiple assemblies as well? How does your model look like?
    Friday, October 19, 2012 5:03 PM
  • EF was failing to generate the enums due to me having declared them inside the Class that was using them. Once I moved the enum declarations outside of the Class, EF was able to 'see' them.

    So, it looks like when EF reflects over your domain model classes to find any declared enums it does not delve into the classes.

    I'm guessing its failing for you due to your enums being declared in a different project - EF might not like this. Try moving your enum declarations into the same namespace as your classes (but not in your classes) and see if that fixes it.

    Monday, October 22, 2012 4:48 AM
  • There is indeed a known issue with enums and entities declared in multiple assemblies. Please check if your scenario is similar: http://entityframework.codeplex.com/workitem/532.

    This posting is provided "AS IS" with no warranties, and confers no rights.


    Monday, October 22, 2012 5:06 AM
    Moderator