简体   繁体   English

多对多 - “无法确定一对一关系的子/依赖方”

[英]Many to Many - "child/dependent side could not be determined for the one-to-one relationship"

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'.无法确定“Artikel.Lager”和“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 http://go.microsoft.com/fwlink/?LinkId=724062 for more details.有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=724062

ER:急诊室:

在此处输入图像描述

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?我到底错过了什么?

You need to replace public Artikel Artikel { get; set; }您需要替换public Artikel Artikel { get; set; } public Artikel Artikel { get; set; } public Artikel Artikel { get; set; } in Lager class and also replace public Lager Lager { get; set; } public Artikel Artikel { get; set; }Lager class 中并替换public Lager Lager { get; set; } public Lager Lager { get; set; } public Lager Lager { get; set; } in Artikel class.Artikel public Lager Lager { get; set; }中。

You'd rather add public LagerArtikel LagerArtikel { get; set; }您宁愿添加public LagerArtikel LagerArtikel { get; set; } public LagerArtikel LagerArtikel { get; set; } public LagerArtikel LagerArtikel { get; set; } in both classes public LagerArtikel LagerArtikel { get; set; }在两个类中

You missed the whole many-to-many:)你错过了整个多对多:)

A Lager will have many Artikels, while an Artikel will have many Lagers.一个贮藏啤酒将有许多Artikel,而一个 Artikel 将有许多贮藏啤酒。

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.由于链接实体 (LagerArtikel) 将具有诸如 Menge 之类的附加属性,因此您需要使用对该实体的引用。 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.如果您只想要一个仅包含 LagerId 和 ArtikelId 的 LagerArtikel 表作为复合 PK 来表示多对多链接,使用 EF6 和 EF Core 5+,您可以取消 LagerArtikel 实体,Lager 可以拥有 Artikel 的集合而 Artikel 有一个 Lagers 集合,EF 可以在其中管理关系和对应表,而无需额外的实体。

Think of it from the table perspective.从表格的角度考虑。 What would you put in the LagerArtikel table?你会在 LagerArtikel 桌子上放什么? 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.它将有一个 LagerId 和一个 ArtikelId,它不能在一行中存储许多lagers 和许多Artikel,而是该表中的每一行将一个 Artikel 链接到一个 Lager。 Many rows will share an ArtikelId, and Many rows will share a LagerId.许多行将共享一个 ArtikelId,许多行将共享一个 LagerId。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM