gpt4 book ai didi

c# - 向 EF : A referential integrity constraint violation occurred 添加新实体时出错

转载 作者:太空狗 更新时间:2023-10-29 20:39:29 26 4
gpt4 key购买 nike

我正在添加一个通过外部类构建的新实体,但出现此错误:发生引用完整性约束冲突:当依赖对象是不变的,除非它被设置为关联的主体对象。主体对象必须被跟踪并且不被标记为删除。 :

编辑:ReconFact 实体未链接到任何其他实体

代码(已编辑):

// the context class
AccountingModelContext db = new AccountingModelContext();
// query some data
List<Reconciliation> recon = db.Reconciliations
.Where(r => r.ReconNum == 112293) // scenario 18
.Include(r => r.ReconciliationDetails.Select(rd => rd.JrnlEntryDetail).Select(jd => jd.JrnlEntry).Select(j => j.ARInvoices.Select(i => i.ARInvoiceDetails)))
.Include(r => r.ReconciliationDetails.Select(rd => rd.JrnlEntryDetail).Select(jd => jd.JrnlEntry).Select(j => j.ARCredMemoes.Select(c => c.ARCredMemoDetails)))
.ToList();
// class that will manager business logic and generate facts with above data
ReconFactGenerator generator = new ReconFactGenerator(recon);
// call to the method that will return a list of facts
List<ReconFact> scenario18 = generator.GenerateFacts();
// ERROR RAISED HERE
scenario18.ForEach(f => db.ReconFacts.Add(f));
// never reached
db.SaveChanges();

这是非常基本的,我使用 ReconFactGenerator 类构建了一个 ReconFact 实体列表。没有指定 ID,因为它们是新实体。

已编辑如果我像这里一样使用相同的方法创建一个新实体,它将完美地工作在我从上下文中查询数据之前:

ReconFact testfact = new ReconFact { ItemCode = "test", ReconNum = 1234, ReconDate = DateTime.Parse("2099-01-01"), ShortName = "L_SZTREDFD", InvoiceAmount = 0, CredMemoAmount = 0, IncomingPayAmount = 0, OutgoingPayAmount = 0, JrnlEntryAmount = 0, OtherAmount = 0 };
db.ReconFacts.Add(testfact); // no problems no error raised
db.SaveChanges(); // I can see the new record in my database
// the ID was generated successfully
// as I've specified identity(1,1) in the database

添加了编辑 2在我像这样从上下文中查询数据之后:

 List<Reconciliation> recon = db.Reconciliations
.Where(r => r.ReconNum == 112293) // scenario 18
.Include(r => r.ReconciliationDetails.Select(rd => rd.JrnlEntryDetail).Select(jd => jd.JrnlEntry).Select(j => j.ARInvoices.Select(i => i.ARInvoiceDetails)))
.Include(r => r.ReconciliationDetails.Select(rd => rd.JrnlEntryDetail).Select(jd => jd.JrnlEntry).Select(j => j.ARCredMemoes.Select(c => c.ARCredMemoDetails)))
.ToList();

上下文无法再保存并返回上述错误。

edited 为什么查询上下文后想保存时报错,之前没有报错?

最佳答案

虽然没有解释错误发生的原因,但找到了解决方法:

// the context class
AccountingModelContext db = new AccountingModelContext();
// query some data
List<Reconciliation> recon = db.Reconciliations
.Where(r => r.ReconNum == 112293) // scenario 18
.Include(r => r.ReconciliationDetails.Select(rd => rd.JrnlEntryDetail).Select(jd => jd.JrnlEntry).Select(j => j.ARInvoices.Select(i => i.ARInvoiceDetails)))
.Include(r => r.ReconciliationDetails.Select(rd => rd.JrnlEntryDetail).Select(jd => jd.JrnlEntry).Select(j => j.ARCredMemoes.Select(c => c.ARCredMemoDetails)))
.ToList();
// class that will manager business logic and generate facts with above data
ReconFactGenerator generator = new ReconFactGenerator(recon);
// call to the method that will return a list of facts
List<ReconFact> scenario18 = generator.GenerateFacts();

// ERROR RAISED HERE
// scenario18.ForEach(f => db.ReconFacts.Add(f));

// INSTEAD OF REUSING THE CONTEXT I CREATE A NEW ONE PROBLEM SOLVED !
using (db = new AccountingModelContext())
{
scenario18.ForEach(f => db.ReconFacts.Add(f));
db.SaveChanges();
}
db.SaveChanges();

关于c# - 向 EF : A referential integrity constraint violation occurred 添加新实体时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20281727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com