我在Visual Studio Community 2015中使用了Entity Framework 6 Code First迁移的MVC 5项目。
我添加了一个新类,并给了该类属性......这是典型的。但是当我去添加脚手架控制器和视图时,我收到错误说"运行所选代码生成器时出错:无法检索[project.class]的元数据。对象引用未设置为对象的实例。"
如果我尝试将DbSet(Of [class])添加到我的Context中,然后在添加scaffolded项之前添加迁移,我会得到同样的事情:"对象引用未设置为对象的实例&# 34。
我不明白它引用的对象引用 - 我所做的只是添加一个属性的类!
更新:
因此,在清洁和建筑工作之后,我最终摆脱了整件事并重新开始。
这次正是我所做的:
1)我创建了我的MVC项目
2)我构建了它然后启用了迁移
3)我将更改提交到存储库
4)我在新课程中添加了属性:
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)我在DbContext中添加了一行,将此类添加为DbSet,以便创建表
6)我添加了一个迁移,并得到了同样的错误。
当它没有工作时,我提交了我的更改,清理,构建并再次尝试....不要再去了。
请注意,我正在尝试将此新类添加到与Identity上下文相同的上下文中。所以,为了看看会发生什么,我创建了一个新项目,添加了完全相同的类,然后尝试创建脚手架控制器和视图到自己的上下文 - 相同的错误。
在我的智慧结束时,这里有一些其他可能有用的信息:
我使用Visual Studio Community 2015获得了Windows 7 64位。
我的项目针对.NET framework 4.5.2。
我不知道这里提供的其他信息可以帮助诊断问题的原因。如果您有任何想法,请告诉我,我会提供相关信息。谢谢!
也许这是VS Community 2015的错误?
我的用户类与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
我的自定义课程" 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
如果该行
公共属性SomeThings As System.Data.Entity.DbSet(Of SomeThing)
被注释掉了,我可以创建一个迁移就好了。当我取消注释时,我就会收到错误。
这是Web Config中的连接字符串:
的ConnectionStrings add name =" IdentityConnection" connectionString ="数据源=(LocalDb)\ v11.0; AttachDbFilename = | DataDirectory | \ aspnet- [project] -20151123105647.mdf; Initial Catalog = aspnet- [project] -20151123105647; Integrated Security = True" 的providerName =" System.Data.SqlClient的" // connectionStrings