- // You can add profile data for the user ...
- public class ApplicationUser : IdentityUser
- {
- public string HomeTown { get; set; }
- public DateTime? BirthDate { get; set; }
- }
如果我想将其称为User而不是ApplicationUser,该怎么办?也许我想添加导航属性,以便我可以与其他模型建立关系?此外,当我查看表时,名称是AspNetUsers而不是ApplicationUser.最后,如果我想使用自己的上下文怎么办?
谢谢你的帮助.
解决方法
1)What if I want to call it User instead of ApplicationUser?
您可以更改为所需的任何名称,只需确保将ApplicationUser替换为名称即可.
2)Maybe I want to add a navigation properties so I can establish
relationship with my other models?
如果您有一个类调用地址,您想将addressId添加为外键,请参阅以下内容:
- public class Address
- {
- public int Id { get; set; }
- public string Street { get; set; }
- }
- public class User : IdentityUser
- {
- public string HomeTown { get; set; }
- public DateTime? BirthDate { get; set; }
- public int AddressId { get; set; }
- [ForeignKey("AddressId")]
- public virtual Address Address { get; set; }
- }
- public class ApplicationDbContext : IdentityDbContext<User>
- {
- public ApplicationDbContext()
- : base("DefaultConnection")
- {
- }
- public DbSet<Address> Addresses { get; set; }
- }
3)Also when I look at the table,the name is AspNetUsers instead of
ApplicationUser.
ASP.NET Identity继承自ASP.NET成员资格系统.注册新用户时
使用默认模板,AspNetUsers和AspNetUserRoles等.默认情况下会创建这些表.
您可以通过修改IdentityModel.cs来修改这些表名.有关更多详细信息,请查看以下链接:
How can I change the table names when using Visual Studio 2013 ASP.NET Identity?
4)What if I want to use my own context?
您可以创建自己的DBContex,MVC 5允许您拥有多个DBContext,例如ApplicationDbContext和DataDbContext(自定义DbContext).
ApplicationDbContext通常包含ASP.NET成员资格数据表.
DataDbContext通常包含与用户无关的数据表.
- public class Blog
- {
- public int BlogId { get; set; }
- public int Title { get; set; }
- }
- public class MyDbContext : DbContext
- {
- public MyDbContext()
- : base("DefaultConnection")
- {
- }
- public DbSet<Blog> Blogs { get; set; }
- }
注意:您可能需要使用EF迁移,请参阅此处的详细信息: