【发布时间】:2014-04-18 00:47:54
【问题描述】:
我正在尝试将 MVC3 应用程序迁移到 MVC5。我用过这些文章:
- http://www.asp.net/identity/overview/migrations/migrating-an-existing-website-from-sql-membership-to-aspnet-identity
- http://www.codeproject.com/Articles/682113/Extending-Identity-Accounts-and-Implementing-Role
旧应用程序使用数据库优先模型(使用 EDMX 文件建模)。
当我尝试更新用户角色时,出现以下错误:
附加信息:实体类型 IdentityRole 不是当前上下文模型的一部分。
在这个方法中(um.AddToRole):
public bool AddUserToRole(string userId, string roleName)
{
var um = new UserManager<AspNetUsers>(
new UserStore<AspNetUsers>(new Entities()));
var idResult = um.AddToRole(userId, roleName);
return idResult.Succeeded;
}
如何告诉 UserManager 使用 AspNetRoles 类?
最好的问候, 马尔科
AspNet 用户
namespace NursingHome.Models
{
using System;
using System.Collections.Generic;
public partial class AspNetUsers
{
public AspNetUsers()
{
this.AspNetUserClaims = new HashSet<AspNetUserClaims>();
this.AspNetUserLogins = new HashSet<AspNetUserLogins>();
this.NursingHome = new HashSet<NursingHome>();
this.AspNetRoles = new HashSet<AspNetRoles>();
}
//public string Id { get; set; }
//public string UserName { get; set; }
//public string PasswordHash { get; set; }
//public string SecurityStamp { get; set; }
public string Discriminator { get; set; }
public System.Guid ApplicationId { get; set; }
public string LegacyPasswordHash { get; set; }
public string LoweredUserName { get; set; }
public string MobileAlias { get; set; }
public bool IsAnonymous { get; set; }
public System.DateTime LastActivityDate { get; set; }
public string MobilePIN { get; set; }
public string Email { get; set; }
public string LoweredEmail { get; set; }
public string PasswordQuestion { get; set; }
public string PasswordAnswer { get; set; }
public bool IsApproved { get; set; }
public bool IsLockedOut { get; set; }
public System.DateTime CreateDate { get; set; }
public System.DateTime LastLoginDate { get; set; }
public System.DateTime LastPasswordChangedDate { get; set; }
public System.DateTime LastLockoutDate { get; set; }
public int FailedPasswordAttemptCount { get; set; }
public System.DateTime FailedPasswordAttemptWindowStart { get; set; }
public int FailedPasswordAnswerAttemptCount { get; set; }
public System.DateTime FailedPasswordAnswerAttemptWindowStart { get; set; }
public string Comment { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<AspNetUserClaims> AspNetUserClaims { get; set; }
public virtual ICollection<AspNetUserLogins> AspNetUserLogins { get; set; }
public virtual ICollection<NursingHome> NursingHome { get; set; }
public virtual ICollection<AspNetRoles> AspNetRoles { get; set; }
}
}
AspNetUsers 扩展
public partial class AspNetUsers : IdentityUser
{
}
AspNetRoles
public partial class AspNetRoles
{
public AspNetRoles()
{
this.AspNetUsers = new HashSet<AspNetUsers>();
}
//public string Id { get; set; }
//public string Name { get; set; }
public virtual ICollection<AspNetUsers> AspNetUsers { get; set; }
}
AspNetRoles 扩展
public partial class AspNetRoles : IdentityRole
{
public AspNetRoles(string name)
: base(name)
{
}
}
数据库上下文扩展
public partial class Entities : IdentityDbContext<AspNetUsers>
{
public Entities()
: base("NursingHomesEntities")
{
}
}
更新
已安装的软件包
- 安装包 Microsoft.AspNet.Identity.EntityFramework
- 安装包 Microsoft.AspNet.Identity.Core
- 安装包实体框架
- 安装包 Microsoft.AspNet.Mvc
- 安装包 Twitter.Bootstrap.MVC
- 安装包 Microsoft.AspNet.Identity.Samples -Pre
- Install-Package FontAwesome
接下来的步骤
- 已添加 EDMX 文件(来自创建的数据库)
- 添加了一张参考 AspNetUser 的表。
现在我收到以下错误:
Model compatibility cannot be checked because the DbContext instance was not created using Code First patterns. DbContext instances created from an ObjectContext or using an EDMX file cannot be checked for compatibility.
难道不能同时使用edmx文件和mvc5吗?
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-5 asp.net-identity