我修改了我的ApplicationUser以包含我的应用程序所需的几个字段。我还设置了存储实体框架中其他对象的上下文,以便使用IdentityDbContext,这样它们就可以共享同一个数据库。一切都很顺利,但是当我去创建用户时会抛出异常:
无法将值NULL插入“Discriminator”列,表'AspNetUsers';列不允许空值。 INSERT失败。 声明已经终止。
这是我的IdentityModels:
Public Class ApplicationUser
Inherits IdentityUser
Public Property FirstName As String
Public Property LastName As String
Public Property OfficePhone As String
Public Property MobilePhone As String
Public Property Administrator As Boolean
Public Property Notifications As Boolean
我的ApplicationDbContext:
Public Class ApplicationDbContext
Inherits IdentityDbContext(Of ApplicationUser)
Public Sub New()
MyBase.New("ExpressConnection", throwIfV1Schema:=False)
End Sub
Public Shared Function Create() As ApplicationDbContext
Return New ApplicationDbContext()
End Function
End Class
这是我的背景:
Public Class MyContext
Inherits IdentityDbContext
Public Sub New()
MyBase.New("DefaultConnection")
End Sub
Public Property Clients As DbSet(Of Client)
Public Property Employees As DbSet(Of ApplicationUser)
Public Property Invoices As DbSet(Of Invoice)
End Class
以下是创建新用户的代码:
根据UserStore对象和ApplicationDbContext对象创建UserManager对象。
Dim userMgr As New UserManager(Of ApplicationUser)(New UserStore(Of ApplicationUser)(context))
Dim appUser As New ApplicationUser
appUser.UserName = "user@example.com"
appUser.Email = "user@example.com"
IdUserResult = userMgr.Create(appUser, "$password$")
每次应用程序启动时都会删除数据库,因此初始迁移应该处理对ApplicationUser的更改。我不确定为什么当我去创建一个新用户时,应用程序会因上述异常而失败。