扩展asp.net的MVC 5的身份与自定义表 [英] Extending asp.net mvc 5 identity with custom tables

查看:133
本文介绍了扩展asp.net的MVC 5的身份与自定义表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net mvc的5应用程序与单个用户帐户的安全性扩展,用户角色和集中用户管理(用户无法创建帐户,只有应用程序管理员,也就是我,创建帐户和分配角色)。

I have an asp.net mvc 5 application with individual user account security extended with user roles and centralized user administration (users can't create accounts, only application administrator, i.e. me, create accounts and assign roles to them).

我的用户类看起来是这样的:

My user class looks something like this:

public class ApplicationUser : IdentityUser
{

// User name, full name, e-mail etc....

}

和它存储在身份提供AspNetUsers表...

and it's stored in "AspNetUsers" table provided by identity...

现在我需要通过添加扩展这一 CompanyID 属性设置为ApplicationUser类和新表公司信息(数据库已经存在)...

Now i need to expand this by adding CompanyID property to a ApplicationUser class and new table with company informations (already existing in database)...

有人能指出我在哪里可以找到信息如何做到这一点?

Can someone point me where to find informations how to do this?

推荐答案

您只需要映射添加到公司的表是这样的:

You just need to add mapping to company table like this:

public class ApplicationUser : IdentityUser
{

// User name, full name, e-mail etc....
    public virtual ICollection<Company> Companies { get; set; }
}

和在公司实体:

public class Company
{

    //other properties
    public virtual ApplicationUser  User { get; set; }
}

然后在你的包管理器控制台中添加由新移民加入迁移命令。
这之后你会得到迁移脚本在迁移文件夹中。

Then add new migration by Add-Migration command in your Package Manager Console. After this moment you will get migration script in your Migrations folder.

然后就叫更新,数据库在同一个控制台上申请迁移到数据库中。

And then just call Update-Database on the same console for applying your migration to database.

这篇关于扩展asp.net的MVC 5的身份与自定义表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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