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