![](/img/trans.png)
[英]EF Core 2: The MERGE statement conflicted with the FOREIGN KEY constraint
[英]EF Core - The MERGE statement conflicted with the FOREIGN KEY constraint
我需要一些帮助来理解我尝试更新产品时遇到的错误。
我已经阅读了这个类似的问题,并尝试了接受的答案(在每个表之后,在最终保存完整产品之前放置一个_context.SaveChanges()
),但我仍然遇到如下所述的相同错误。
这些是涉及的模型:
public class Product
{
public int Id { get; set; }
// some more properties
public ICollection<IdentifierForProduct> Identifiers { get; set; }
}
public class IdentifierForProduct
{
public int Id { get; set; }
public int ProductId { get; set; }
public int ProductIdentifierId { get; set; }
public string Value { get; set; } // E.g. "4902505154881"
public ProductIdentifier Identifier { get; set; }
public Product Product { get; set; }
}
public class ProductIdentifier
{
public int Id { get; set; }
public string Label { get; set; } // E.g. "EAN"
public ICollection<IdentifierForProduct> ProductIdentifiers { get; set; }
}
最初,在表单发布后,设置Identifiers
( VMProduct
是产品视图模型):
List<IdentifierForProduct> Identifiers = new List<IdentifierForProduct>();
if (VMProduct.Identifiers != null)
{
for (var i = 0; i < VMProduct.Identifiers.Count; i++)
{
Identifiers.Add(new IdentifierForProduct
{
ProductId = VMProduct.Id,
ProductIdentifierId = VMProduct.Identifiers[i].Id,
Value = VMProduct.Identifiers[i].Value
});
}
}
然后根据表单中所做的更改更改产品属性:
Product DbM = await GetProduct(VMProduct.Id);
// some more properties are set
DbM.Identifiers = Identifiers;
_context.Update(DbM);
await _context.SaveChangesAsync();
此异常在await _context.SaveChangesAsync();
抛出await _context.SaveChangesAsync();
:
SqlException:MERGE 语句与 FOREIGN KEY 约束“FK_IdentifiersForProducts_ProductIdentifiers_ProductIdentifierId”冲突。 冲突发生在数据库“MyStore”、表“dbo.ProductIdentifiers”、“Id”列中。 该语句已终止。 System.Data.SqlClient.SqlCommand+<>c.b__108_0(任务结果)
DbUpdateException: 更新条目时出错。 有关详细信息,请参阅内部异常。 Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch+d__32.MoveNext()
这是GetProduct()
方法:
public async Task<Product> GetProduct(int Id)
{
Product DbM = await _context.Products
.Include(ic => ic.InCategories)
.ThenInclude(pc => pc.ProductCategory)
.Include(t => t.Type)
.ThenInclude(i => i.Identifiers) // ProductIdentifiersInTypes
.ThenInclude(i => i.Identifier)
.Include(t => t.Type)
.ThenInclude(p => p.Properties) // ProductPropertiesInTypes
.ThenInclude(p => p.Property)
.ThenInclude(o => o.Options)
.Include(p => p.ProductPropertyOptions)
.Where(p => p.Id == Id)
.SingleOrDefaultAsync();
return DbM;
}
发生此错误的原因是,您在“IdentifierForProduct”中的外键“ProductIdentifierId”可能在此处的值为 0:
List<IdentifierForProduct> Identifiers = new List<IdentifierForProduct>();
if (VMProduct.Identifiers != null)
{
for (var i = 0; i < VMProduct.Identifiers.Count; i++)
{
Identifiers.Add(new IdentifierForProduct
{
ProductId = VMProduct.Id,
ProductIdentifierId = VMProduct.Identifiers[i].Id, //here, your id should be 0
Value = VMProduct.Identifiers[i].Value
});
}
}
当实体框架核心遇到外键值为0时,它会抛出这种错误,因为它无法插入作为某个对象的主键的外值0。 显然,主键的值不能为 0。
希望,这回答了你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.