Sql ASPNET.Identity和MVC5
我正在尝试使用新的身份和MVC5构建身份验证,但我想“加入”一个单独的用户表到注册和管理页面。我读过很多关于扩展AspNetUser表的文章,但是我非常想知道如何加入一个单独的表。我不想只向AspNetUser表中添加字段,因为这个项目将是其他网站应用程序的模板,每个项目对用户表都有不同的要求,因此我相信,如果我能够调整单独的表,而不是不断更改AspNetUser表,将会更加精简 任何代码示例都将不胜感激,因为我通过示例学习比解释更好Sql ASPNET.Identity和MVC5,sql,asp.net-mvc,c#-5.0,entity-framework-6,asp.net-identity,Sql,Asp.net Mvc,C# 5.0,Entity Framework 6,Asp.net Identity,我正在尝试使用新的身份和MVC5构建身份验证,但我想“加入”一个单独的用户表到注册和管理页面。我读过很多关于扩展AspNetUser表的文章,但是我非常想知道如何加入一个单独的表。我不想只向AspNetUser表中添加字段,因为这个项目将是其他网站应用程序的模板,每个项目对用户表都有不同的要求,因此我相信,如果我能够调整单独的表,而不是不断更改AspNetUser表,将会更加精简 任何代码示例都将不胜感激,因为我通过示例学习比解释更好 Alex由于ASP.NET Identity首先使用EF代码
Alex由于ASP.NET Identity首先使用EF代码,您可以通过添加新的代码优先类并将其添加到DbContext,将您的配置文件存储为单独的表。下面的帖子展示了你如何做到这一点@Alex这就是我所拥有的,它确实对我有用 这将创建一个从IdentityUser继承的自定义用户类,就像ApplicationUser一样,它还定义了一个虚拟类,该虚拟类是在数据库中创建用户时链接的关联概要文件类。这可以作为一个自定义解决方案在这里完成,为了使它使用现有的表,您只需使用数据库中的实体对象替换这里的对象,并使用指向正确数据库的连接字符串
public class CustomUser : IdentityUser
{
public virtual CustomUserProfile CustomUserProfile { get; set; }
}
public class CustomUserProfile
{
[Key]
public Guid Id
{
get { return Guid.NewGuid(); }
private set { value = Id; }
}
public string FirstName { get; set; }
public string LastName { get; set; }
//add more items
}
public class ApplicationDbContext : IdentityDbContext<CustomUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public System.Data.Entity.DbSet<CustomUserProfile> CustomUserProfile { get; set; }
}
如果您在迁移过程中仍然遇到问题,那么您可以删除MDF文件并重新运行应用程序,然后它将重新创建数据库。先编码还是先模型?谢谢这一点-我已经比以前走得更远了-但是,尽管我可以在注册表上看到我的新字段,提交时收到以下错误消息:自创建数据库以来,支持“ApplicationDbContext”上下文的模型已更改。考虑使用代码第一迁移来更新数据库。我不想先写代码!我已经有一个数据库,默认连接是连接到它;或者在我第一次设置ASPNET身份时,它就这样做了。谢谢你。已经这么做了。我在等待来自pranav的更多信息,事实上,pranav在另一个博客中报告说,他们正在为这个精确的场景编写进一步的文档。有一个codeplex项目正在进行中,以使其首先成为数据库,但我不确定它的地位。
public class CustomUser : IdentityUser
{
public virtual CustomUserProfile CustomUserProfile { get; set; }
}
public class CustomUserProfile
{
[Key]
public Guid Id
{
get { return Guid.NewGuid(); }
private set { value = Id; }
}
public string FirstName { get; set; }
public string LastName { get; set; }
//add more items
}
public class ApplicationDbContext : IdentityDbContext<CustomUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public System.Data.Entity.DbSet<CustomUserProfile> CustomUserProfile { get; set; }
}
var user = new CustomUser() { UserName = model.UserName };
user.CustomUserProfile = new CitadelUserProfile();
user.CustomUserProfile.FirstName = model.FirstName;
var result = UserManager.Create(user, model.Password);