如何将外键设置为多个表

时间:2019-03-24 18:29:31

标签: c# entity-framework-core

我有一个Articolo班和一个Variante班。问题是如何配置引用Articolo和Variante的Stock类的外键 我的产品清单如下: 第1-variant1 第1-variant2 第1个变体3 第2条 ... 等等

在Esistenze中,我有 第1-variante1-5条 第1-variante2-10条 第1-variante3-100 Article2-noVariante-0

class Articolo{
 public string Codart { get; set; }//key
  ...
 public List<Variante> Varianti { get; set; }
 public Esistenza Esistenza { get; set; }
}

class Variante{
 public string Codart { get; set; }//key
 public string Variante { get; set; }//key
}

class Esistenza
{
 public string Codart { get; set; }//key
 public string Variante { get; set; }//key
 public int Esistenza { get; set; }

public Articolo Articolo { get; set; }

}

//sqlite
CREATE TABLE Esistenze( 
[Codart] TEXT NOT NULL, 
[Codvar] TEXT NOT NULL, 
[Esiste] REAL NOT NULL, 
CONSTRAINT [PK_Esistenze] PRIMARY KEY ( [Codart], [Codvar] ), 
CONSTRAINT FK_Articoli FOREIGN KEY (Codart) REFERENCES Articoli(Codart) ON DELETE CASCADE, 
CONSTRAINT FK_Varianti FOREIGN KEY (Codvar) REFERENCES Varianti(Codvar) ON DELETE CASCADE 
)

public void Configure(EntityTypeBuilder<Esistenza> builder)
        {
            builder.HasKey(e => new {e.Codart, e.Codvar});
            builder.Property(e => e.Esiste).IsRequired();

             builder
           .HasOne(a => a.Articolo)
           .WithOne(e => e.Esistenza)
           .HasForeignKey<Esistenza>(f => new {f.RkaCodart,f.RkaCodvar});
 }

我得到错误 具有外键属性{'Codart':string,'Codvar':string}的从'Esistenza.Articolo'到'Articolo.Esistenze'的关系不能针对主键{'Codart':string},因为它不兼容

0 个答案:

没有答案