简体   繁体   English

实体框架ApplicationUser在一个类中不止一次

[英]Entity Framework ApplicationUser more than once in a class

EF: 6.0 Code First .NET: 4.5.2 VS2015 EF: 6.0代码优先。NET: 4.5.2 VS2015

I need the scenario where 2 users are on a Board . 我需要2个用户在Board There can be many boards, so Tom could be a Grantor on a board and Mary be a Claimer . 董事会可以有很多,所以汤姆可以是董事会的Grantor ,玛丽可以是Claimer Yet on another board Mary could be a Grantor and Tom could be a Claimer . 然而在另一个董事会上,玛丽可能是Grantor ,汤姆可能是Claimer

To get around there being 2 ApplicationUsers in the same model, I derived from ApplicationUser : 为了避开同一模型中有2个ApplicationUsers,我派生自ApplicationUser

[Table("GrantorUsers")]
public class Grantor : ApplicationUser
{
    public Grantor()
    {
        GrantBoards = new List<Board>();
    }

    public ICollection<Board> GrantBoards { get; set; }
}

[Table("ClaimerUsers")]
public class Claimer : ApplicationUser
{
    public Claimer()
    {
        ClaimBoards = new List<Board>();
    }
    public ICollection<Board> ClaimBoards { get; set; }
}

And then created the Board class: 然后创建Board类:

public class Board
{
    public Board()
    {
        TaskItems = new List<TaskItem>();
        BoardSharedUsers = new HashSet<ApplicationUser>();
    }

    public int Id { get; set; }

    public string Name { get; set; }

    public string ClaimerId { get; set; }
    [ForeignKey("ClaimerId")]
    public Claimer Claimer { get; set; }

    public string GrantorId { get; set; }
    [ForeignKey("GrantorId")]
    public Grantor Grantor { get; set; }

    public int TotalPoints { get; set; }

    public ICollection<TaskItem> TaskItems { get; set; }

    public ICollection<ApplicationUser> BoardSharedUsers { get; set; }
}

And added the DbSet of: 并添加了以下DbSet:

public IDbSet<Board> Boards { get; set; }

This added 2 tables to the db of GrantorUsers and ClaimerUsers with only ID fields. 这将2个表添加到具有ID字段的GrantorUsers和ClaimerUsers的数据库中。

Is this the wrong way of looking designing this schema? 这是设计此架构的错误方式吗? Or if it is correct, how can Tom be an ApplicationUser all the time, but then when I create a board make him either a Grantor or a Claimer? 或者,如果正确的话,Tom怎么会一直都是ApplicationUser,但是当我创建董事会时,让他成为Grantor或Claimer?

var tom = db.Users.Find(id);
var board = new Board {
    GrantorId = tom.Id,
    //...initialize other properties
};

var grantor = new Grantor(tom); //add a copy constructor
                                //and how tough to copy all the
                                //inherited properties from
                                //ApplicationUser's parent base classes

context.Users.Attach(grantor);
context.Boards.Add(board);
context.SaveChanges();

This is the idea I'm after, but know it must be done in a different way. 这是我追求的主意,但知道必须以其他方式完成。 On the next board Tom could be a Claimer and Mary a Claimer. 在下一局中,汤姆可能是索赔人,玛丽可能是索赔人。

I think it's not exactly the c# question but anyway. 我认为这不完全是C#问题,但无论如何。 You need use roles, user groups. 您需要使用角色,用户组。 Example of "Fast way": In database store table "Roles" with struct |UserId|BoardId|Role|. “快速”示例:在数据库存储表“ Roles”中具有结构| UserId | BoardId | Role |。 When "Tom" visit a board your programm check this table and assign proper role. 当“汤姆”访问您的程序板时,请检查此表并分配适当的角色。 So he can have different roles for different boards. 因此,他可以在不同的董事会中扮演不同的角色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架父级子级-子级多次引用父级 - Entity Framework Parent Child - Child refers to parent more than once 查询的结果不能被枚举多次(C#)(实体框架)(存储过程) - The Result of a Query Cannot be Enumerated More than Once (C#) (Entity Framework) (Stored Procedure) 我们可以在实体框架中附加多个对象并进行一次保存更改吗 - Can we attach more than one object in entity framework and do savechanges once 如何使用Entity Framework检索在db中多次出现的单个记录? - How to retrieve single records that occurs more than once in db using Entity Framework? 实体框架MappingException:类型&#39;XXX已被多次映射 - Entity framework MappingException: The type 'XXX has been mapped more than once 一次保存多个实体 - Saving more than one entity at once 使用 EF Core 多次添加相同的实体 object - Adding same entity object more than once with EF Core 实体框架7 DbContext OnModelCreating为ApplicationUser字段指定外键 - Entity Framework 7 DbContext OnModelCreating specify foreign key for ApplicationUser field mvc和带有ApplicationUser表的实体框架中的一对一或一对零关系 - one to one or one to zero relationship in mvc and entity framework with ApplicationUser table 首先使用ApplicationUser的实体框架代码创建约束或键 - Creating a constraint or key with Entity Framework Code First to ApplicationUser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM