我将从这个问题开始,因为有很多额外的细节:
如何将IDENT2.0配置为使用TPT扩展的ApplicationUser,包括成功地将POST发送到~/Account/
我一直在使用DB FirstE 211方法在VB.NET中使用最新的EF和Identity 2.0来开发MVC 5 VB.NET项目。
我实现了一个自定义标识2.0实现,它使用整数键,而不是默认的GUID/String。(我相信this是我使用的指南,记住我正在为我的项目把所有的东西都翻译成VB.NET,我是C#能干的,但是VB.NET MVC中的一些奇怪的差异仍然影响着我)
我昨天第一次注意到我的问题时,我试图注册一个帐户使用~/帐户/注册,并有99%的肯定,因为它以前的工作,因为我已经能够在系统上创建一个用户帐户以前。我收到的错误是:
One or more validation errors were detected during model generation:
ApplicationUser_Logins_Target: : Multiplicity is not valid in Role 'ApplicationUser_Logins_Target' in relationship 'ApplicationUser_Logins'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.有错误的操作:
它似乎是由对(New ApplicationDbContext()).Users的任何调用引起的,而后者又调用了.Users,而后者又给出了错误。
相关行动
<Authorize> _
Public Function Index() As ActionResult
Dim model As List(Of EditUserViewModel) = New List(Of EditUserViewModel)()
Using db As ApplicationDbContext = New ApplicationDbContext()
' Get list of Users
Dim users As IEnumerable(Of ApplicationUser) = db.Users.ToList()
Dim models As IEnumerable(Of EditUserViewModel) = From userr In users Select New EditUserViewModel(userr)
model.AddRange(models)
'Return View(New List(Of EditUserViewModel)(db.Users.Select(Function(u) New EditUserViewModel(u)))) 'UNSUPPORTED IN LINQ TO ENTITIES
End Using
Return View(model)
End Function
<HttpPost>
<ValidateAntiForgeryToken>
Public Async Function Register(model As RegisterViewModel) As Task(Of ActionResult)
If ModelState.IsValid Then
If (Await UserManager.CreateAsync(model.GetUser(), model.Password)).Succeeded Then
Return RedirectToAction("Index", "Account")
End If
End If
' If we got this far, something failed, redisplay form
Return View(model)
End Function我的身份模型设置如下:
Public Class ApplicationUser
Inherits IdentityUser(Of Integer, CustomUserLogin, CustomUserRole, CustomUserClaim)
Implements IUser(Of Integer)
Public Async Function GenerateUserIdentityAsync(manager As CustomUserManager) As Task(Of ClaimsIdentity)
Dim userIdentity = Await manager.CreateIdentityAsync(Me, DefaultAuthenticationTypes.ApplicationCookie)
' Add custom user claims here
Return userIdentity
End Function
Public Property FirstName As String
Public Property LastName As String
End Class
Public Class ApplicationDbContext
Inherits IdentityDbContext(Of ApplicationUser, CustomRole, Integer, CustomUserLogin, CustomUserRole, CustomUserClaim)
Public Sub New()
MyBase.New("DBNAME")
End Sub
Public Shared Function Create() As ApplicationDbContext
Return New ApplicationDbContext()
End Function
Protected Overrides Sub OnModelCreating(ByVal modelBuilder As DbModelBuilder)
MyBase.OnModelCreating(modelBuilder)
modelBuilder.Entity(Of ApplicationUser).ToTable("Users").HasKey(Function(x) New With {x.Id})
'modelBuilder.Entity(Of User).ToTable("Users").HasKey(Function(x) New With {x.Id})
'modelBuilder.Entity(Of Client).ToTable("Clients").HasKey(Function(x) New With {x.Id})
'modelBuilder.Entity(Of Provider).ToTable("Providers").HasKey(Function(x) New With {x.Id})
modelBuilder.Entity(Of CustomUserRole).ToTable("UserRoles").HasKey(Function(x) New With {x.RoleId, x.UserId})
modelBuilder.Entity(Of CustomUserLogin).ToTable("UserLogins").HasKey(Function(x) New With {x.UserId})
modelBuilder.Entity(Of CustomUserClaim).ToTable("UserClaims")
modelBuilder.Entity(Of CustomRole).ToTable("Roles")
End Sub
End Class
Public Class IdentityManager
Public Function RoleExists(name As String) As Boolean
Dim rm = New CustomRoleManager(New RoleStore(Of IdentityRole)(New ApplicationDbContext()))
Return rm.RoleExists(name)
End Function
Public Function CreateRole(name As String) As Boolean
Dim rm = New CustomRoleManager(New RoleStore(Of IdentityRole)(New ApplicationDbContext()))
Dim idResult = rm.Create(New CustomRole(name))
Return idResult.Succeeded
End Function
Public Function CreateUser(user As ApplicationUser, password As String) As Boolean
Dim um = New CustomUserManager(New CustomUserStore(New ApplicationDbContext()))
Dim idResult = um.Create(user, password)
Return idResult.Succeeded
End Function
Public Function AddUserToRole(userId As String, roleName As String) As Boolean
Dim um = New CustomUserManager(New CustomUserStore(New ApplicationDbContext()))
Dim idResult = um.AddToRole(userId, roleName)
Return idResult.Succeeded
End Function
Public Sub ClearUserRoles(userId As String)
Dim um = New CustomUserManager(New CustomUserStore(New ApplicationDbContext()))
Dim user = um.FindById(userId)
Dim currentRoles = New List(Of CustomUserRole)()
currentRoles.AddRange(user.Roles)
For Each role As CustomUserRole In currentRoles
um.RemoveFromRole(userId, role.RoleId)
Next
End Sub
End Class
'New drived classes
Public Class CustomUserRole
Inherits IdentityUserRole(Of Integer)
End Class
Public Class CustomUserClaim
Inherits IdentityUserClaim(Of Integer)
End Class
Public Class CustomUserLogin
Inherits IdentityUserLogin(Of Integer)
End Class
Public Class CustomRole
Inherits IdentityRole(Of Integer, CustomUserRole)
Public Sub New()
End Sub
Public Sub New(name__1 As String)
Name = name__1
End Sub
End Class
Public Class CustomUserStore
Inherits UserStore(Of ApplicationUser, CustomRole, Integer, CustomUserLogin, CustomUserRole, CustomUserClaim)
Public Sub New(context As ApplicationDbContext)
MyBase.New(context)
End Sub
End Class
Public Class CustomRoleStore
Inherits RoleStore(Of CustomRole, Integer, CustomUserRole)
Public Sub New(context As ApplicationDbContext)
MyBase.New(context)
End Sub
End Class我的TPT设置如下(Db生成的类)
Partial Public Class User
Public Property Id As Integer
Public Property FirstName As String
Public Property LastName As String
Public Property Email As String
Public Property EmailConfirmed As Boolean
Public Property PasswordHash As String
Public Property SecurityStamp As String
Public Property PhoneNumber As String
Public Property PhoneNumberConfirmed As Boolean
Public Property TwoFactorEnabled As Boolean
Public Property LockoutEndDateUtc As Nullable(Of Date)
Public Property LockoutEnabled As Boolean
Public Property AccessFailedCount As Integer
Public Property UserName As String
Public Property Mobile As String
Public Property Address1 As String
Public Property Address2 As String
Public Property Town As String
Public Property County As String
Public Property PostCode As String
Public Property Created As Nullable(Of Date)
Public Property Modified As Nullable(Of Date)
Public Property Deleted As Nullable(Of Date)
Public Overridable Property CarXMLs As ICollection(Of CarXML) = New HashSet(Of CarXML)
Public Overridable Property Notification As Notification
Public Overridable Property Queries As ICollection(Of Query) = New HashSet(Of Query)
Public Overridable Property UserClaims As ICollection(Of UserClaim) = New HashSet(Of UserClaim)
Public Overridable Property UserLogins As ICollection(Of UserLogin) = New HashSet(Of UserLogin)
Public Overridable Property Roles As ICollection(Of Role) = New HashSet(Of Role)
Public Overridable Property Requests As ICollection(Of Request) = New HashSet(Of Request)
End Class
Partial Public Class Client
Inherits User
End Class
Partial Public Class Provider
Inherits User
Public Property CompanyName As String
Public Property ContractAccepted As Nullable(Of Date)
Public Property TermsAccepted As Nullable(Of Date)
Public Property RequestEmail As String
Public Property ClientTextGeneral As String
Public Property ClientTextOpeningHours As String
Public Property ClientTextLocation As String
Public Property FeeDiscountQuote As Nullable(Of Decimal)
Public Property FeeDiscountBuy As Nullable(Of Decimal)
Public Property FeeDiscountFlat As Nullable(Of Decimal)
Public Property Balance As Nullable(Of Decimal)
Public Property QuoteReplyCost As Nullable(Of Decimal)
Public Property MultiServiceDiscount As Nullable(Of Decimal)
Public Overridable Property Quotes As ICollection(Of Quote) = New HashSet(Of Quote)
Public Overridable Property ServicesInProviders As ICollection(Of ServicesInProvider) = New HashSet(Of ServicesInProvider)
Public Overridable Property Transactions As ICollection(Of Transaction) = New HashSet(Of Transaction)
End Class堆栈跟踪
[ModelValidationException: One or more validation errors were detected during model generation:
ApplicationUser_Logins_Target: : Multiplicity is not valid in Role 'ApplicationUser_Logins_Target' in relationship 'ApplicationUser_Logins'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.
]
System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate() +259
System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) +447
System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +103
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +143
System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +171
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +594
System.Data.Entity.Internal.InternalContext.Initialize() +31
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +39
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +137
System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() +38
System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +108
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +369
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
Quotes4YouVB.AccountController.Index() in \\SEMA4NET20\d\websites\Quotes4YouVB\Quotes4YouVB Master\Controllers\AccountController.vb:31
lambda_method(Closure , ControllerBase , Object[] ) +101
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +434
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +60
System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +76
System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +36
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +73
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +117
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +323
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +44
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +72
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651156
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155发布于 2014-09-18 09:57:25
我不确定为什么修复它,但我刚刚发现了解决方案:
从ApplicationDbContext.OnModelCreating()表中删除.HasKey()
Protected Overrides Sub OnModelCreating(ByVal modelBuilder As DbModelBuilder)
MyBase.OnModelCreating(modelBuilder)
modelBuilder.Entity(Of ApplicationUser).ToTable("Users") '.HasKey(Function(x) New With {x.Id})
modelBuilder.Entity(Of CustomUserRole).ToTable("UserRoles") '.HasKey(Function(x) New With {x.RoleId, x.UserId})
modelBuilder.Entity(Of CustomUserLogin).ToTable("UserLogins") '.HasKey(Function(x) New With {x.UserId})
modelBuilder.Entity(Of CustomUserClaim).ToTable("UserClaims")
modelBuilder.Entity(Of CustomRole).ToTable("Roles")
End Sub令人恼火的是,我相信我记得添加了这些内容,专门为我解决了一个带有身份的问题。我会尽快更新这个答案,只要我知道更多的解决方案,但请随时评论,如果你认为你知道。
https://stackoverflow.com/questions/25846907
复制相似问题