Project.Models说,我有一个MVC 5项目,在这个项目中,我将我的模型分离成一个离开的项目。身份( Identity,ApplicationUser)模型及其DbContext仍然在主项目中,正如默认的MVC 5项目支架中所显示的那样。Project.Models程序集中的一个实体必须具有一个属性,该属性是ApplicationUsers的列表。
现在,我开始认为将实体模型分离成一个单独的项目不是一个好主意,因为现在我有了独立的上下文和程序集中的实体模型和Idenity模型。
如果ApplicaitonUsers是在单独的程序集中,那么如何使它们成为实体对象的一部分?我应该将项目合并回一个程序集和DbContexgt,还是有一个简单的方法来解决这个问题?我不能在DbSet<MainProject.ApplicationUser>中定义DbContext of Project.Models,因为这意味着循环引用。
谢谢。
发布于 2015-08-25 15:17:18
您可以在模型项目中添加与身份相关的包Microsoft.AspNet.Identity和Microsoft.AspNet.Identity.EntityFramework,并将ApplicationUser类移动到模型项目。
还可以在ApplicationUser中添加配置文件数据,如下所示:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, string> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}另外,要使它成为一个单一的DBContext,可以在dbcontext类中直接从dbcontext继承。如下所示:
public partial class MyDBContext : IdentityDbContext<ApplicationUser>
{
public MyDBContext()
: base("name=DefaultConnection", throwIfV1Schema: false)
{
}
}这样,您就不需要在上下文类中定义DbSet<MainProject.ApplicationUser>。
https://stackoverflow.com/questions/32205762
复制相似问题