我正在嘗試將MVC3應用程序遷移到MVC5。我已經使用了這些文章:告訴UserManager使用AspNetRoles
- 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文件建模)。
當我嘗試更新用戶的角色我得到了以下錯誤:
Additional information: The entity type IdentityRole is not part of the model for the current context.
在這種方法(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 Extensi在
public partial class AspNetRoles : IdentityRole
{
public AspNetRoles(string name)
: base(name)
{
}
}
DB上下文擴展
public partial class Entities : IdentityDbContext<AspNetUsers>
{
public Entities()
: base("NursingHomesEntities")
{
}
}
更新
安裝的軟件包
- 安裝,包裝Microsoft.AspNet.Identity.EntityFramework
- 安裝,包裝Microsoft.AspNet .Identity.Core
- 安裝,包裝的EntityFramework
- 安裝,包裝Microsoft.AspNet.Mvc
- 安裝,包裝Twitter.Bootstrap.MVC
- 安裝,包裝Microsoft.AspNet.Identity.Samples - 預
- 安裝,包裝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在一起嗎?