我正在尝试将MVC3应用程序迁移到MVC5。我使用了这些文章:
旧的应用程序使用数据库优先模型(使用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类?
向你问好,马尔科
AspNetUsers
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)
{
}
}DB上下文扩展
public partial class Entities : IdentityDbContext<AspNetUsers>
{
public Entities()
: base("NursingHomesEntities")
{
}
}更新
已安装的软件包
下一步
现在我得到了以下错误:
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吗?
发布于 2014-03-13 23:21:40
首先,我建议使用一个新的asp.net标识2.0测试版:https://www.nuget.org/packages/Microsoft.AspNet.Identity.Core/2.0.0-beta1
它更好,更灵活的覆盖和使用。您可以在这里获得一些信息、示例和其他内容:http://blogs.msdn.com/b/webdev/archive/2014/02/11/announcing-preview-of-microsoft-aspnet-identity-2-0-0-beta1.aspx
我无法重现此错误,但可能是映射配置中的一个实体框架错误,而不是asp.net MVC错误。您需要注意,在您的应用程序中替换dbcontext的所有用法,有一个主题要查看以避免此错误: is not part of the model for the current context
如果仍然不起作用,试着做一些测试和经验,或者在这里添加更多信息。
https://stackoverflow.com/questions/22376013
复制相似问题