Home > Back-end >  Many to Many - "child/dependent side could not be determined for the one-to-one relationship&qu
Many to Many - "child/dependent side could not be determined for the one-to-one relationship&qu

Time:11-24

I am trying to achieve a many-many-relationship, but I get:

The child/dependent side could not be determined for the one-to-one relationship between 'Artikel.Lager' and 'Lager.Artikel'. To identify the child/dependent side of the relationship, configure the foreign key property. If these navigations should not be part of the same relationship configure them without specifying the inverse. See enter image description here

Code:

  #region Data

    [Table("Lager")]
    public class Lager
    {
        public Guid Id { get; set; } = Guid.NewGuid();
        public string Name { get; set; } = "";
        public string Strasse { get; set; } = "";
        public string PLZ { get; set; } = "";
        public string Ort { get; set; } = "";

        public Artikel Artikel { get; set; }
    }

    [Table("LagerArtikel")]
    public class LagerArtikel
    {
        public Guid Id { get; set; } = Guid.NewGuid();

        //[ForeignKey("Lager")]
        //public Guid LagerId { get; set; }

        //[ForeignKey("Artikel")]
        //public Guid ArtikelId { get; set; }

        public int Menge { get; set; }

        public ICollection<Artikel> Artikels { get; set; }
        public ICollection<Lager> Lagers { get; set; }
    }

    [Table("Artikel")]
    public class Artikel
    {
        public Guid Id { get; set; } = Guid.NewGuid();
        public string Name { get; set; } = "";
        public decimal EinkaufspreisNettoEuro { get; set; }
        public Lager Lager { get; set; }
    }

    #endregion

    #region Context

    // => EF Core
    /*
    Add-Migration Initial -context _1_Testing.XDBContextTesting -o Migrations\XDBContextTestingMig
    add-migration -Name A3 -Project compDatMVP -context _1_Testing.XDBContextTesting
    Update-Database -context _1_Testing.XDBContextTesting 
    */

    public class XDBContextTesting : DbContext
    {
        public DbSet<Lager> Lager { get; set; }
        public DbSet<LagerArtikel> LagerArtikel { get; set; }
        public DbSet<Artikel> Artikel { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(new string(Konstanten.ConnectionString.Replace("#db#", "compDat__1__Testing")));
        }

        public XDBContextTesting() : base()
        {
        }

        public XDBContextTesting(DbContextOptions<XDBContextTesting> options) : base(options)
        {
        }
    }

    #endregion

What did I miss exactly?

CodePudding user response:

You missed the whole many-to-many :)

A Lager will have many Artikels, while an Artikel will have many Lagers.

public class Lager
{
   [Key]
   public int LagerId { get; set; }
   // ...
   public virtual ICollection<LagerArtikel> LagerArtikels { get; set; } = new List<LagerArtikel>();
}

public class Artikel
{
   [Key]
   public int ArtikelId { get; set; }
    // ...
   public virtual ICollection<LagerArtikel> LagerArtikels { get; set; } = new List<LagerArtikel>();
}

public class LagerArtikel
{
    [Key]
    public int LagerArtikelId {get; set;}
    [ForeignKey("Lager")]
    public int LagerId {get; set;}
    [ForeignKey("Artikel")]
    public int ArtikelId {get; set;}

    // or just use a composite PK:
    //[Key, Column(Order=1), ForeignKey("Lager")]
    //public int LagerId {get; set;}
    //[Key, Column(Order=2), ForeignKey("Artikel")]
    //public int ArtikelId {get; set;}

    public virtual Lager Lager { get; set; }
    public virtual Artikel Artikel { get; set; }
    public int Menge { get; set; }
}

Since the linking entity (LagerArtikel) will have additional properties like Menge you need to use a reference to this entity. If you just want a LagerArtikel table with just the LagerId and ArtikelId as a composite PK to represent the many-to-many link, with EF6 and EF Core 5 you could do away with the LagerArtikel entity and Lager could have a collection of Artikels while Artikel has a collection of Lagers, where EF can manage the relationship and corresponding table without the extra entity.

Think of it from the table perspective. What would you put in the LagerArtikel table? It would have a LagerId and an ArtikelId, it cannot store many lagers and many Artikels in one row, instead each row in this table links an Artikel to a Lager. Many rows will share an ArtikelId, and Many rows will share a LagerId.

CodePudding user response:

You need to replace public Artikel Artikel { get; set; } in Lager class and also replace public Lager Lager { get; set; } in Artikel class.

You'd rather add public LagerArtikel LagerArtikel { get; set; } in both classes

  • Related