Sti*_*ian 8 c# entity-framework-core asp.net-core
我需要一些帮助来理解我尝试更新产品时遇到的错误。
我已经阅读了这个类似的问题,并尝试了接受的答案(_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; }
}
Run Code Online (Sandbox Code Playgroud)
最初,在表单发布之后,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
});
}
}
Run Code Online (Sandbox Code Playgroud)
然后根据表单中所做的更改更改产品属性:
Product DbM = await GetProduct(VMProduct.Id);
// some more properties are set
DbM.Identifiers = Identifiers;
_context.Update(DbM);
await _context.SaveChangesAsync();
Run Code Online (Sandbox Code Playgroud)
抛出此异常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;
}
Run Code Online (Sandbox Code Playgroud)
Ibr*_*nov 17
发生此错误的原因是“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
});
}
}
Run Code Online (Sandbox Code Playgroud)
当 Entity Framework Core 遇到外键值 0 时,它会抛出此类错误,因为它无法插入作为某个对象的主键的外值 0。显然,主键不能值为 0。
| 归档时间: |
|
| 查看次数: |
8320 次 |
| 最近记录: |