最后我想通了,我会在这里发布基础知识,以防其他人受益。
在我自己的数据类中,我继承了 DbContext 并为 Identity 表添加了 DbSets,并在末尾添加了 onModelCreating 子:
Public Class MyDataClass
Inherits DbContext
'Identity Tables
Public Property AspNetRoles As DbSet(Of IdentityRole)
Public Property AspNetUserClaims As DbSet(Of IdentityUserClaim)
Public Property AspNetUserLogins As DbSet(Of IdentityUserLogin)
Public Property AspNetUserRoles As DbSet(Of IdentityUserRole)
Public Property AspNetUsers As DbSet(Of ApplicationUser)
'All of my other data...
Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
modelBuilder.Entity(Of IdentityRole)().HasKey(Of String)(Function(r) r.Id).[Property](Function(p) p.Name).IsRequired()
modelBuilder.Entity(Of IdentityUserRole)().HasKey(Function(r) New With { _
r.RoleId, _
r.UserId _
})
modelBuilder.Entity(Of IdentityUserLogin)().HasKey(Function(u) New With { _
u.UserId, _
u.LoginProvider, _
u.ProviderKey _
})
End Sub
End Class
在 IdentityModels.vb 中,我删除了将基础定义为 DefaultConnection 的部分,留下以下内容:
Imports System
Imports Microsoft.AspNet.Identity.EntityFramework
Imports exportApp.exportApp.models
Imports System.Data.Entity
Imports System.ComponentModel.DataAnnotations
Imports System.ComponentModel.DataAnnotations.Schema
' You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
Public Class ApplicationUser
Inherits IdentityUser
Property emailAddress() As String
Overridable Property clientCompany() As ClientCompany
End Class
我创建了一个类 ApplicationDbContext。注意MyDataClass 中的New() 子:
Imports Microsoft.AspNet.Identity.EntityFramework
Imports System.Data.Entity
Namespace exportApp.models
Public Class ApplicationDbContext
Inherits DbContext
Public Overridable Property Users() As IDbSet(Of ApplicationUser)
Get
Return m_Users
End Get
Set(value As IDbSet(Of ApplicationUser))
m_Users = value
End Set
End Property
Private m_Users As IDbSet(Of ApplicationUser)
Public Sub New()
MyBase.New("MyDataClass")
End Sub
Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
If modelBuilder Is Nothing Then
Throw New ArgumentNullException("modelBuilder")
End If
modelBuilder.Configurations.AddFromAssembly(GetType(models.Configurations.ApplicationUserConfiguration).Assembly)
End Sub
End Class
End Namespace
最后,我在“Models”文件夹中添加了一个“Configurations”文件夹,并添加了以下五个类。这是我之前缺少的部分。
ApplicationUserConfiguration.vb:
Imports Microsoft.AspNet.Identity.EntityFramework
Imports System.Collections.Generic
Imports System.Data.Entity.ModelConfiguration
Imports System.Linq
Imports System.Web
Namespace myApp.models.Configurations
Public Class ApplicationUserConfiguration
Inherits EntityTypeConfiguration(Of ApplicationUser)
Public Sub New()
[Property](Function(u) u.UserName).IsRequired()
HasMany(Of IdentityUserRole)(Function(u) u.Roles)
End Sub
End Class
End Namespace
IdentityRoleConfiguration.vb
Imports Microsoft.AspNet.Identity.EntityFramework
Imports System.Collections.Generic
Imports System.Data.Entity.ModelConfiguration
Imports System.Linq
Imports System.Web
Namespace myApp.models.Configurations
Public Class IdentityRoleConfiguration
Inherits EntityTypeConfiguration(Of IdentityRole)
Public Sub New()
[Property](Function(r) r.Name).IsRequired()
End Sub
End Class
End Namespace
IdentityUserClaimConfiguration.vb
Imports Microsoft.AspNet.Identity.EntityFramework
Imports System.Collections.Generic
Imports System.Data.Entity.ModelConfiguration
Imports System.Linq
Imports System.Web
Namespace myApp.models.Configurations
Public Class IdentityUserClaimConfiguration
Inherits EntityTypeConfiguration(Of IdentityUserClaim)
Public Sub New()
HasRequired(Function(u) u.User)
End Sub
End Class
End Namespace
IdentityUserLoginConfiguration.vb
Imports Microsoft.AspNet.Identity.EntityFramework
Imports System.Collections.Generic
Imports System.Data.Entity.ModelConfiguration
Imports System.Linq
Imports System.Web
Imports System.Data.Entity
Namespace myApp.models.Configurations
Public Class IdentityUserLoginConfiguration
Inherits EntityTypeConfiguration(Of IdentityUserLogin)
Public Sub New()
HasKey(Function(l) New With { _
l.UserId, _
l.LoginProvider, _
l.ProviderKey _
})
HasRequired(Of IdentityUser)(Function(u) u.User)
End Sub
End Class
End Namespace
IdentityUserRoleConfiguration.vb
Imports Microsoft.AspNet.Identity.EntityFramework
Imports System.Collections.Generic
Imports System.Data.Entity.ModelConfiguration
Imports System.Linq
Imports System.Web
Namespace CustomUser.Models.Configurations
Public Class IdentityUserRoleConfiguration
Inherits EntityTypeConfiguration(Of IdentityUserRole)
Public Sub New()
HasKey(Function(r) New With { _
r.UserId, _
r.RoleId _
})
End Sub
End Class
End Namespace
我想就是这样。添加迁移,更新数据库,然后我就走了。
希望这可以帮助其他人,如果有人可以贡献,或者提出更好的方法,我很乐意谈谈!