[英]Entity Framework ApplicationUser more than once in a class
EF: 6.0代碼優先。NET: 4.5.2 VS2015
我需要2個用戶在Board
。 董事會可以有很多,所以湯姆可以是董事會的Grantor
,瑪麗可以是Claimer
。 然而在另一個董事會上,瑪麗可能是Grantor
,湯姆可能是Claimer
。
為了避開同一模型中有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; }
}
然后創建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; }
}
並添加了以下DbSet:
public IDbSet<Board> Boards { get; set; }
這將2個表添加到具有ID字段的GrantorUsers和ClaimerUsers的數據庫中。
這是設計此架構的錯誤方式嗎? 或者,如果正確的話,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();
這是我追求的主意,但知道必須以其他方式完成。 在下一局中,湯姆可能是索賠人,瑪麗可能是索賠人。
我認為這不完全是C#問題,但無論如何。 您需要使用角色,用戶組。 “快速”示例:在數據庫存儲表“ Roles”中具有結構| UserId | BoardId | Role |。 當“湯姆”訪問您的程序板時,請檢查此表並分配適當的角色。 因此,他可以在不同的董事會中扮演不同的角色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.