1

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!


UPDATE:

So after cleaning and building didn't work, 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 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??


My User class with DbContext:

Imports Microsoft.AspNet.Identity.EntityFramework
Imports System.Threading.Tasks
Imports System.Security.Claims
Imports Microsoft.AspNet.Identity

Public Class AppUser
    Inherits IdentityUser

    'Email and Phone inherit from IdentityUser

    Private firstNameStr As String
    Public Property FirstName() As String
        Get
            Return firstNameStr
        End Get
        Set(value As String)
            firstNameStr = value
        End Set
    End Property

    Private lastNameStr As String
    Public Property LastName() As String
        Get
            Return lastNameStr
        End Get
        Set(value As String)
            lastNameStr = value
        End Set
    End Property

    Private favoritesStr As List(Of String)
    Public Property Favorites() As List(Of String)
        Get
            Return favoritesStr
        End Get
        Set(value As List(Of String))
            favoritesStr = value
        End Set
    End Property

    Public Async Function GenerateUserIdentityAsync(manager As UserManager(Of AppUser)) As Task(Of ClaimsIdentity)
        ' Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        Dim userIdentity = Await manager.CreateIdentityAsync(Me, DefaultAuthenticationTypes.ApplicationCookie)
        ' Add custom user claims here
        Return userIdentity
    End Function
End Class

Public Class MyAppIdentityDbContext
    Inherits IdentityDbContext(Of AppUser)

    Public Sub New()
        MyBase.New("IdentityConnection", throwIfV1Schema:=False)
    End Sub

    Public Shared Function Create() As MyAppIdentityDbContext
        Return New MyAppIdentityDbContext()
    End Function

    Public Property SomeThings As System.Data.Entity.DbSet(Of SomeThing)
End Class

My custom class "SomeThing":

Public Class SomeThing

    Private thingIDInt As Integer
    Private nameStr As String
    Private latitudeDbl As Double
    Private longitudeDbl As Double
    Private metadataStr As List(Of String)
    Private logoImg As Image
    Private myAppUser As AppUser

    Public Property SomeThingID() As Integer
        Get
            Return thingIDInt
        End Get
        Set(ByVal value As Integer)
            thingIDInt = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return nameStr
        End Get
        Set(value As String)
            nameStr = value
        End Set
    End Property

    Public Property Latitude() As Double
        Get
            Return latitudeDbl
        End Get
        Set(value As Double)
            latitudeDbl = value
        End Set
    End Property

    Public Property Longitude() As Double
        Get
            Return longitudeDbl
        End Get
        Set(value As Double)
            longitudeDbl = value
        End Set
    End Property

    Public Property SearchTerms() As List(Of String)
        Get
            Return metadataStr
        End Get
        Set(value As List(Of String))
            metadataStr = value
        End Set
    End Property

    Public Property Logo() As Image
        Get
            Return logoImg
        End Get
        Set(value As Image)
            logoImg = value
        End Set
    End Property

    Public Property MyUser() As AppUser
        Get
            Return myAppUser
        End Get
        Set(value As AppUser)
            myAppUser = value
        End Set
    End Property

End Class

If the line

Public Property SomeThings As System.Data.Entity.DbSet(Of SomeThing)

is commented out, I can create a migration just fine. Soon as I uncomment it I get that error.

This is connection string in Web Config:

connectionStrings add name="IdentityConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-[project]-20151123105647.mdf;Initial Catalog=aspnet-[project]-20151123105647;Integrated Security=True" providerName="System.Data.SqlClient" / /connectionStrings

Andy
  • 616
  • 11
  • 32
  • Did you build your project before doing this? – DavidG Nov 19 '15 at 21:37
  • @DavidG - yes, I did. Thank you, tho! – Andy Nov 19 '15 at 22:09
  • Lot's of possible causes for that. Before scaffolding, make sure you can just reference the new class var myObjects = context.MyClass.ToList(); If that fails, look at your DbSet code, web config, etc. – Steve Greene Nov 20 '15 at 15:59
  • @SteveGreene - thank you for the info. At least in my 2nd new project (see UPDATE) 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....do you know of a way? Intellisense does see my new class as a legitimate type, but I can't get a list of it from my context as in your suggestion because it is erroring when I try to add it to a DbContext (whether the existing Identity context, or its own new one). – Andy Nov 21 '15 at 01:34
  • Post your context. Could be a subtle typo. – Steve Greene Nov 21 '15 at 16:13
  • @SteveGreene - I updated my question with my user class and dbcontext plus the custom class and the web.config string. Just note, though, even if all I do is add that custom class (so everything else is the default scaffolding), I get the same thing. Thanks! – Andy Nov 23 '15 at 16:21
  • Are you setting an initializer anywhere? You need to tell EF to either drop and recreate your DB or to use migrations. Try adding this to your App_Start in global.asax: Database.SetInitializer(Of MyAppIdentityDbContext)(New DropCreateDatabaseAlways(Of MyAppIdentityDbContext)) http://stackoverflow.com/questions/8679562/place-to-put-database-setinitializer – Steve Greene Nov 23 '15 at 17:07
  • @SteveGreene - thank you for the suggestion! Unfortunately that did not help. Actually, I found the issue: apparently it does not like trying to add properties of type Image or List (of anything) to the DB. I commented both of those properties out and was able to add a migration. I will have to come up with a work-around (the list is easy, but not sure what to do about the image - if you have any suggestions there, I'd greatly appreciate it!). – Andy Nov 23 '15 at 18:56
  • http://stackoverflow.com/questions/4653095/how-to-store-images-using-entity-framework-code-first-ctp-5 – Steve Greene Nov 24 '15 at 15:04

0 Answers0