微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在尝试在 the External Authentication Services (C#)上完成本教程.我需要一些初步的解释才能继续.查看MVC5的认模板,我看到了这个:
// 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迁移,请参阅此处的详细信息:

ASP.Net Identity customizing UserProfile

原文地址:https://www.jb51.cc/aspnet/247718.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐