none
EF Code First NullReferenceException when add new Model RRS feed

  • Question


  • I have an MVC 5 project using Entity Framework 6 Code First migrations in Visual Studio Community 2015.

    I added a new class and gave that class properties...so typical. But when I go to add the scaffolded controller and views, I get an error saying "There was an error running the selected code generator: Unable to retrieve metadata for [project.class]. Object reference not set to an instance of an object."

    If I try adding the DbSet(Of [class]) to my Context and then adding a migration before adding the scaffolded items, I get the same thing: "Object reference not set to an instance of an object".

    I don't understand what object reference it's referring to - all I did was add a class with properties!

    Cleaning and building didn't work, and ultimately I ended up getting rid of the whole thing and starting over.

    This time here is exactly what I did:

    1) I created my MVC project

    2) I built it then enabled migrations

    3) I committed my changes to repository

    4) I added my new class with properties:

    Public Property ID() As Integer
    Public Property Name() As String
    Public Property Latitude() As Double
    Public Property Longitude() As Double
    Public Property SearchTerms() As List(Of String)
    Public Property Logo() As Image

    5) I added the line in the DbContext to add this class as a DbSet so it will create the table

    6) I added a migration, and got the same error.

    When that didn't work, I committed my changes, cleaned, built, and tried again....no go.

    Note that I am trying to add this new class into the same context as my Identity context. So just to see what would happen, I created a new project, added the exact same class, then tried creating scaffolded controller and views into its own context - SAME ERROR.

    At my wits end, here is some other info that MAY help:

    I have Windows 7 64-bit using Visual Studio Community 2015.

    My project is targeting .NET framework 4.5.2.

    I did not change web config or ANYTHING....just created the project and added the class.  It would be nice if there were a way to get more detailed information from this error....is there a way? Intellisense does see my new class as a legitimate type, but because it is erroring when I try to add it to a DbContext (whether the existing Identity context, or its own new one) I can't do anything useful with it.

    I do not know what other information to provide here that could help diagnose the cause of the issue. If you have an idea, please let me know and I will provide that info. Thank you!

    Maybe it's a bug with VS Community 2015??

    • Moved by Jiayi Li Monday, November 23, 2015 7:57 AM
    Saturday, November 21, 2015 2:01 AM

All replies

  • Hi Andarta,

    This forum is discuss Visual Studio WPF/SL Designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System, and Visual Studio Editor.

    Your issue would be related to ADO.NET Entity Framework and LINQ to Entities   , I will help you move this case to this forum for dedicated support. Thank you for your understanding.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    Click HERE to participate the survey.

    Monday, November 23, 2015 7:45 AM
  • Hello Andarta,

    According to your description, I also made a test by below steps:

    1.Create a MVC project

    2.Enable the migration and create the database

    3.Add a new entity class and add the line in the DbContext(it should be in ApplicationDbContext)

    4.add a migration to add the table into the database. And call the update-database command to commit the upgrade, however, on my side, the result is successful.

    So I am a bit interesting about why the same steps would have a different result. Could you please share your project without enabling the migration and the entity type you want to add it later, I would like make a test with your demo to see if I could reproduce it.

    By the way, I used the Visual Studio Community 2015 on my side, too.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 24, 2015 3:30 AM
    Moderator