【发布时间】:2022-01-02 17:31:45
【问题描述】:
我正在尝试实现多对多关系,但我得到:
无法确定一对一的孩子/依赖方 'Artikel.Lager' 和 'Lager.Artikel' 之间的关系。识别 关系的子/依赖方,配置外部 关键财产。如果这些导航不应该是相同的一部分 关系配置它们而不指定反向。看 http://go.microsoft.com/fwlink/?LinkId=724062 了解更多详情。
ER:
代码:
#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
我到底错过了什么?
【问题讨论】:
标签: c# sql entity-framework migration ef-core-6.0