如何ASPNET身份与我的模型 [英] How AspNet Identity with my model

查看:96
本文介绍了如何ASPNET身份与我的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图去通过本教程的<一个href=\"http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on\"相对=nofollow>外部认证服务(C#)。我需要一些初步的解释往前走。展望默认模板里面附带MVC5,我看到:

  //您可以为用户添加配置文件数据...
公共类ApplicationUser:IdentityUser
{
    公共字符串的家乡{搞定;组; }
    公众的DateTime?出生日期{搞定;组; }
}

如果我想怎么称呼它用户而不是 ApplicationUser ?也许我想添加一个导航属性​​,所以我可以与我的其他车型建立关系?此外,当我看看表,名称为 AspNetUsers 而不是 ApplicationUser 。最后,如​​果我想要用我自己的背景?

感谢您的帮助。


解决方案

  

1)如果我想称之为用户而不是ApplicationUser?


您可以切换到任何你想要的名称,只要确定的名称来代替ApplicationUser。


  

2)也许我想添加一个导航属性​​,所以我可以建立
  与我的其他机型的关系?


如果你有一个一流的呼叫地址,你要添加addressId作为外键,见下图:

 公共类地址
{
    公众诠释标识{搞定;组; }
    公共字符串街{搞定;组; }
}公共类用户:IdentityUser
{
    公共字符串的家乡{搞定;组; }
    公众的DateTime?出生日期{搞定;组; }
    公众诠释AddressId {搞定;组; }
    [ForeignKey的(AddressId)]
    公共虚拟地址地址{搞定;组; }
}公共类ApplicationDbContext:IdentityDbContext&lt;使用者&GT;
{
    公共ApplicationDbContext()
        :基地(DefaultConnection)
    {
    }
    公共DbSet&LT;地址&gt;地址{搞定;组; }
}


  

3)此外,当我看看表,名字是AspNetUsers代替
  ApplicationUser。


ASP.NET身份从ASP.NET成员系统继承。当您注册一个新用户
使用默认模板,AspNetUsers和AspNetUserRoles等。这些表是默认创建的。
您可以通过修改IdentityModel.cs修改这些表名。欲了解更多详细看看以下链接:

<一个href=\"http://stackoverflow.com/questions/19460386/how-can-i-change-the-table-names-when-using-visual-studio-2013-aspnet-identity\">How使用Visual Studio 2013 ASPNET身份时,我可以更改表名?


  

4)如果我想用我自己的背景?


您可以创建自己的DBContex,MVC 5让您拥有多发的DbContext,如ApplicationDbContext和DataDbContext(自定义的DbContext)。
ApplicationDbContext通常包含ASP.NET成员资格数据表。
DataDbContext通常包含无关的用户数据表。

 公共类博客
{
    公众诠释BlogId {搞定;组; }
    公众诠释标题{搞定;组; }
}公共类MyDbContext:的DbContext
{
    公共MyDbContext()
        :基地(DefaultConnection)
    {    }
    公共DbSet&LT;博客&GT;博客{搞定;组; }
}

注意:您可能需要使用EF迁移,详情请参阅这里:

<一个href=\"http://stackoverflow.com/questions/19814432/asp-net-identity-customizing-userprofile/19822728#19822728\">ASP.Net身份定制用户配置

I'm trying to go through this tutorial on the External Authentication Services (C#). I need some initial explanations to go forward. Looking inside the default template that ships MVC5, I see this:

// You can add profile data for the user ...
public class ApplicationUser : IdentityUser
{
    public string HomeTown { get; set; }
    public DateTime? BirthDate { get; set; }
}

What if I want to call it User instead of ApplicationUser? Maybe I want to add a navigation properties so I can establish relationship with my other models? Also when I look at the table, the name is AspNetUsers instead of ApplicationUser. Finally, What if I want to use my own context?

Thanks for helping.

解决方案

1)What if I want to call it User instead of ApplicationUser?

You can change to any names you want, just make sure to replace ApplicationUser with the name.

2)Maybe I want to add a navigation properties so I can establish relationship with my other models?

If you have a class call Address, you want to add addressId as a foreign key, see below:

 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 is inherited from the The ASP.NET membership system. When you register a new user using the default template, AspNetUsers and AspNetUserRoles etc.. these tables are created by default. You can modify these table name by modifying the IdentityModel.cs. For more detail take a look at the following link:

How can I change the table names when using Visual Studio 2013 AspNet Identity?

4)What if I want to use my own context?

You can create your own DBContex, MVC 5 allow you to have mutiple DBContext, such as ApplicationDbContext and DataDbContext(custom DbContext). ApplicationDbContext usually contains ASP.NET Membership data table. DataDbContext usually contains data tables unrelated to users.

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; }
}

Note: You may need to use EF Migrations, see details here :

ASP.Net Identity customizing UserProfile

这篇关于如何ASPNET身份与我的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆