[英]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.