我想添加很多项目到EF,以便将它们放到数据库中,但我有这个错误(在标题中给出)。我在网上搜索,但我只找到了一些基于一个项目不能被添加两次的事实的想法,这似乎是合乎逻辑的。但我注意到给出了不同的Id (Id是属性,它是关联表中的键),但它不起作用,我仍然有这个错误。到目前为止,我还不知道会出什么问题。
下面是我尝试推送到数据库的数据,在不同的模型类之后:
最大的容器: Supermarche
[DataContract]
public class Supermarche
{
[Key]
[DataMember]
public int SupermarcheId { set; get; }
[DataMember]
public virtual ObservableCollection<Magasin> Magasins { set; get; }
}它包含一些"Magasin":
[DataContract]
public class Magasin : ElementSupermarche
{
[Key]
[DataMember]
public int MagasinId { set; get; }
[DataMember]
public string Nom { set; get; }
[DataMember]
public virtual ObservableCollection<Rayon> Rayons { set; get; }
}它包含了一些“人造丝”:
[DataContract]
public class Rayon : ElementSupermarche
{
[Key]
[DataMember]
public int RayonId { set; get; }
[DataMember]
public string Nom { set; get; }
[DataMember]
public virtual ObservableCollection<ProduitMagasin> Produits { set; get; }
}其中包含一些"ProduitMagasin":
[DataContract]
public class ProduitMagasin : ElementSupermarche
{
[Key]
[DataMember]
public int ProduitMagasinId { set; get; }
[DataMember]
public string Nom { set; get; }
[DataMember]
public int Quantite { set; get; }
}最后,但并非最不重要的是填充部分:
using (var ctx = new MarketContext("sqlserver"))
{
new MyDataInitializer().InitializeDatabase(ctx);
var produitMagasin1 = new ProduitMagasin() { Nom = "Pommes", Quantite = 10 };
var produitMagasin2 = new ProduitMagasin() { Nom = "Poires", Quantite = 5 };
var rayon1 = new Rayon() { RayonId = 1, Nom = "Fruits & légumes", Produits = new ObservableCollection<ProduitMagasin>() { produitMagasin1, produitMagasin2 } };
var produitMagasin5 = new ProduitMagasin() { Nom = "pizzas", Quantite = 4 };
var produitMagasin6 = new ProduitMagasin() { Nom = "quiches", Quantite = 8 };
var rayon3 = new Rayon() { RayonId = 2, Nom = "Surgelés", Produits = new ObservableCollection<ProduitMagasin>() { produitMagasin1, produitMagasin2 } };
var magasin1 = new Magasin() { Nom = "Auchan", Rayons = new ObservableCollection<Rayon>() { rayon1, rayon3 } };
ctx.SaveChanges();
var produitMagasin3 = new ProduitMagasin() { Nom = "melons", Quantite = 13 };
var produitMagasin4 = new ProduitMagasin() { Nom = "fraises", Quantite = 37 };
var rayon2 = new Rayon() { RayonId = 3, Nom = "Fruits & légumes", Produits = new ObservableCollection<ProduitMagasin>() { produitMagasin3, produitMagasin4 } };
var magasin2 = new Magasin() { Nom = "Carrefour", Rayons = new ObservableCollection<Rayon>() { rayon2 } };
var supermarche1 = new Supermarche() { Magasins = new ObservableCollection<Magasin> { magasin1, magasin2 } };
ctx.Supermarches.Add(supermarche1);
ctx.SaveChanges();
}我不认为这是相关的,但这里是在WCF服务中的pull部分,它紧跟在上面的代码之后执行(在不同的上下文中,但可能存在一些时间问题,可能是计算机在将样本信息推送到数据库之前到达了pull部分:
using (var ctx2 = new MarketContext("sqlserver"))
{
ctx2.Configuration.ProxyCreationEnabled = false;
var sm = ctx2.Supermarches
.Include(s => s.Magasins.Select(mg => mg.Rayons.Select(r => r.Produits)))
.First();
return sm;
}谢谢
发布于 2020-01-23 03:56:39
解决后,添加RayonId=2的人造丝与添加RayonId=1的粘胶含有相同的产物。
https://stackoverflow.com/questions/59867276
复制相似问题