我想通了最后,我将在这里发布的基本知识的情况下,它可以造福别人。
在我自己的数据类,我继承的DbContext并添加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
最后,我添加了一个“配置”文件夹中的“模型”文件夹,并添加以下五个类别。这是我以前失踪的部分。
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
我想这就是它。添加迁移,更新数据库,并且我离开了。
希望这可以帮助其他人,如果任何人都可以贡献,或建议更好的方法,我很乐意谈!