Peter Hansen
Peter Hansen

Reputation: 103

2 many to many relationship between 2 classes

public class LootItem
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Ilvl { get; set; }
    public ItemSlot slot { get; set; }
    public virtual ICollection<LootPlayer> Player { get; set; }
    public virtual ICollection<LootPlayer> PlayerBis { get; set; }
    public LootItem()
    {
        Player = new List<LootPlayer>();
        PlayerBis = new List<LootPlayer>();
    }
}

public class LootPlayer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public LootClass PlayerClass { get; set; }
    public LootRole PlayerRole { get; set; }
    public virtual ICollection<LootItem> CurrentGear { get; set; }
    public virtual ICollection<LootItem> BisGear { get; set; }
    public LootPlayer()
    {
        CurrentGear = new List<LootItem>();
        BisGear = new List<LootItem>();
    }
}

I'm trying to create 2 x many to many relations between these 2 tables.
Player <-> CurrentGear
PlayerBis <-> BisGear

Is this posible, because currently when I run the Update-Database I don't get any relationship tables. Only LootItem and LootPlayer

Upvotes: 0

Views: 64

Answers (1)

Farhad Zamani
Farhad Zamani

Reputation: 5861

You Can't create many-to-many relationship. you should create two one-to-many relationship like this. create an class like this

public class LootPlayerLootItem
{
    public int Id { get; set; }
    public int PlayerId { get; set; }
    public int PlayerBisId { get; set; }
    public int CurrentGearId { get; set; }
    public int BisGearId { get; set; }
    [ForeignKey("CurrentGearId")]
    public LootPlayer CurrentGear  { get; set; }
    [ForeignKey("BisGearId")]
    public LootPlayer BisGear { get; set; }
    [ForeignKey("PlayerId")]
    public LootItem Player { get; set; }
    [ForeignKey("PlayerBisId")]
    public LootItem PlayerBis { get; set; }
}

then change the LootItem and LootPlayer to this

public class LootItem
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Ilvl { get; set; }
    public ItemSlot slot { get; set; }
    [InverseProperty("Player")]
    public virtual ICollection<LootPlayerLootItem> Player { get; set; }
    [InverseProperty("PlayerBis")]
    public virtual ICollection<LootPlayerLootItem> PlayerBis { get; set; }
    public LootItem()
    {
        Player = new List<LootPlayerLootItem>();
        PlayerBis = new List<LootPlayerLootItem>();
    }
}

public class LootPlayer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public LootClass PlayerClass { get; set; }
    public LootRole PlayerRole { get; set; }
    [InverseProperty("CurrentGear")]
    public virtual ICollection<LootPlayerLootItem> CurrentGear { get; set; }
    [InverseProperty("BisGear")]
    public virtual ICollection<LootPlayerLootItem> BisGear { get; set; }
    public LootPlayer()
    {
        CurrentGear = new List<LootPlayerLootItem>();
        BisGear = new List<LootPlayerLootItem>();
    }
}

Upvotes: 1

Related Questions