EFより: 6.0コードファースト.NET: 4.5.2 VS2015Entity FrameworkのApplicationUser一度クラスの
私は2人のユーザーがBoard
上にあるシナリオを必要とします。多くのボードが存在する可能性がありますので、トムはボード上にGrantor
、メアリーはClaimer
となります。しかし、別のボードでは、メアリーはGrantor
、トムはClaimer
になる可能性があります。
[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; }
}
をそして理事会のクラスを作成しました::
同じモデルで2するApplicationUsersている周りのそこに、私はApplicationUser
由来
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; }
これにより、GrantorUsersおよびClaimerUsersのデータベースに2つのテーブルが追加されましたIDフィールドのみ。
このスキーマを設計するのは間違っていますか?それとも正しいのか、いつTomがApplicationUserになれるのですか?しかし、私がボードを作って、贈与者か請求者のどちらかにしますか?
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();
これは私が後にしているアイデアですが、別の方法で行う必要があることはわかります。次のボードでは、トムは主張者でありメアリーは主張者である可能性があります。