如何实现IdentityRole(dbo.AspNetRoles)和自定义实体之间的关系? [英] How to implement a relationship between IdentityRole (dbo.AspNetRoles) and a custom entity?

查看:133
本文介绍了如何实现IdentityRole(dbo.AspNetRoles)和自定义实体之间的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用与MVC模板项目类似的项目。



我创建了一些我想在数据库中表示的模型。我可以用DbContext类创建它们,但问题是将我的类的RoleId与ASP.Net Identity的角色表的id相连接。



解决方案

让我们说 RoleDependent 是你的实体类。然后:



1)如果您需要一对多(1:N)关系 bebetween RoleDependent 角色

  public class RoleDependent 
{
public Int32 Id {get;组; }
public virtual IdentityRole IdentityRole {get;组;
}

将导致以下迁移:

  CreateTable(dbo.RoleDependents,
c => new
{
Id = c.Int(nullable: false,identity:true),
IdentityRole_Id = c.String(maxLength:128),
})
.PrimaryKey(t => t.Id)
.ForeignKey dbo.AspNetRoles,t => t.IdentityRole_Id)
.Index(t => t.IdentityRole_Id);

2)如果您需要一对一(1:1)关系 bebetween RoleDependent 角色

  public class RoleDependent 
{
public String Id {get;组; }
[ForeignKey(Id)]
public virtual IdentityRole IdentityRole {get;组;
}

这将导致迁移:

  CreateTable(dbo.RoleDependents,
c => new
{
Id = c.String(nullable:false ,maxLength:128),
})
.PrimaryKey(t => t.Id)
.ForeignKey(dbo.AspNetRoles,t => t.Id)
.Index(t => t.Id);


I'm working with a project similar to the MVC template project.

I've created some models that I want to be represented in the database. I can create them with a DbContext class just fine, the issue is connecting my class's RoleId with ASP.Net Identity's Role table's id.

Any ideas as to how this is possible?

解决方案

Let's say that RoleDependent is your entity class. Then:

1) If you need one-to-many (1:N) relationship beetween RoleDependent and Role:

public class RoleDependent
{
    public Int32 Id { get; set; }
    public virtual IdentityRole IdentityRole { get; set; }
}

that will result in the following migration:

CreateTable("dbo.RoleDependents",
c => new
    {
        Id = c.Int(nullable: false, identity: true),
        IdentityRole_Id = c.String(maxLength: 128),
    })
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AspNetRoles", t => t.IdentityRole_Id)
.Index(t => t.IdentityRole_Id);

2) If you need one-to-one (1:1) relationship beetween RoleDependent and Role:

public class RoleDependent
{
    public String Id { get; set; }
    [ForeignKey("Id")]
    public virtual IdentityRole IdentityRole { get; set; }
}

which would result in the migration:

CreateTable("dbo.RoleDependents",
    c => new
        {
            Id = c.String(nullable: false, maxLength: 128),
        })
    .PrimaryKey(t => t.Id)
    .ForeignKey("dbo.AspNetRoles", t => t.Id)
    .Index(t => t.Id);

这篇关于如何实现IdentityRole(dbo.AspNetRoles)和自定义实体之间的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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