asp.net-mvc – AspNet如何与我的模型识别

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – AspNet如何与我的模型识别前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在 the External Authentication Services (C#)上完成本教程.我需要一些初步的解释才能继续.查看MVC5的默认模板,我看到了这个:
  1. // You can add profile data for the user ...
  2. public class ApplicationUser : IdentityUser
  3. {
  4. public string HomeTown { get; set; }
  5. public DateTime? BirthDate { get; set; }
  6. }

如果我想将其称为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添加为外键,请参阅以下内容

  1. public class Address
  2. {
  3. public int Id { get; set; }
  4. public string Street { get; set; }
  5. }
  6.  
  7. public class User : IdentityUser
  8. {
  9. public string HomeTown { get; set; }
  10. public DateTime? BirthDate { get; set; }
  11. public int AddressId { get; set; }
  12. [ForeignKey("AddressId")]
  13. public virtual Address Address { get; set; }
  14. }
  15.  
  16. public class ApplicationDbContext : IdentityDbContext<User>
  17. {
  18. public ApplicationDbContext()
  19. : base("DefaultConnection")
  20. {
  21. }
  22. public DbSet<Address> Addresses { get; set; }
  23. }

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通常包含与用户无关的数据表.

  1. public class Blog
  2. {
  3. public int BlogId { get; set; }
  4. public int Title { get; set; }
  5. }
  6.  
  7. public class MyDbContext : DbContext
  8. {
  9. public MyDbContext()
  10. : base("DefaultConnection")
  11. {
  12.  
  13. }
  14. public DbSet<Blog> Blogs { get; set; }
  15. }

注意:您可能需要使用EF迁移,请参阅此处的详细信息:

ASP.Net Identity customizing UserProfile

猜你在找的asp.Net相关文章