私は、コンピュータとユーザーとの関係が多岐にわたっています。残念ながら、多対多の関係にCRUD操作の例はあまりありません。最も重要な追加と削除。私はcomputer.ComputerUser.Add()のようにしようとしましたが、それは動作しますが、削除は私のために混乱しています。私はそれを正しくやっているかどうかわからない。あなたの洞察は助けになります。ありがとうございますエンティティフレームワークのコア2.0のコードを使用して、多対多の関係でCRUD操作を行う
// a computer can have one or more Users registered to access
public class Computer
{
[Key]
public Guid ComputerId { get; set; }
public string MachineName { get; set; }
public DateTime Updated { get; set; }
public bool IsActive { get; set; } = true;
public virtual ICollection<ComputerUser> ComputerUser { get; set; }
public Computer()
{
this.ComputerUser = new HashSet<ComputerUser>();
}
}
// an user can have access to one or more computers
public class User
{
[Key]
public Guid GatekeeperUserId { get; set; }
public string Name { get; set; }
public virtual ICollection<ComputerUser> ComputerUser { get; set; }
public GatekeeperUser()
{
this.ComputerUser= new HashSet<ComputerUser>();
}
}
// Joining table
[Key]
public Guid ComputerId { get; set; }
public Computer Computer { get; set; }
[Key]
public Guid UserId { get; set; }
public User User { get; set; }
ああ、ComputerUserをDbSetに追加する必要がありますか?私はナビゲーションを介して行うことはできません – Raj
はい、このテーブルはDbSetにもあるはずです – MateuszWkDev
私はComputerUserブリッジテーブルを2つのテーブルに分割してそれぞれに多くの情報を格納する方法はありますか?ありがとうございました – Raj