EF Core - MERGE语句与FOREIGN KEY约束冲突

时间:2018-05-02 08:54:28

标签: c# asp.net-core entity-framework-core

我需要一些帮助来理解我在尝试更新产品时遇到的错误。

我已阅读this similar question,并尝试了接受的答案(在最终保存完整产品之前,在每张表后放置_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();上会抛出此异常:

  

SqlException:MERGE语句与FOREIGN KEY约束“FK_IdentifiersForProducts_ProductIdentifiers_ProductIdentifierId”冲突。冲突发生在数据库“MyStore”,表“dbo.ProductIdentifiers”,列“Id”中。   该语句已终止。   System.Data.SqlClient.SqlCommand +&lt;&gt; 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;
}

1 个答案:

答案 0 :(得分:1)

发生此错误的原因是,“ 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。

希望,这可以回答您的问题。