【问题标题】:Entity Framework On Update : Store update, insert, or delete statement affected an unexpected number of rows实体框架更新:存储更新、插入或删除语句影响了意外数量的行
【发布时间】:2017-11-01 16:05:47
【问题描述】:

我有以下型号:-

    public abstract class BaseClass
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public Guid Id
            {
                get;
                set;
            }protected BaseClass()
            {
                if (Guid.Empty==Id)
                {
                    Id = LongGuid.NewGuid();

                }
            }
        }
    }

    public class Product : BaseClass
        {

          // other properties like name and price.
          private Recipe _recipe;
            public virtual Recipe Recipe
            {
                get
                {    
                    return this._recipe;
                }
                set
                {
                    this._recipe = value;
                }
            }

            private InventoryItem _inventoryItem;
            public virtual InventoryItem InventoryItem
            {
                get
                {
                    return this._inventoryItem;
                }
                set
                {
                    this._inventoryItem = value;
                }
            }
public class InventoryItem  : BaseClass
{
 // Name and value 
}

public class Recipe : BaseClass
{
 // Name and value 
}
public class DataContext : DbContext
{
    public DbSet<Recipe> Recipes { get; set; }
    public DbSet<InventoryItem> InventoryItems { get; set; }
    public DbSet<Product> Product { get; set; }
}

我保存如下:-

this._workspace.Update(Model);
this._context.SaveChanges();

当我使用 InventoryItem 和 Recipe 创建产品时,它运行良好。

但是当我创建没有 InventoryItem 和 Recipe 的产品并保存在数据库中时,一段时间后我尝试使用新的 InventoryItem () 和新的 Recipe () 更新产品时,我遇到了以下错误:-

保存不公开外键的实体时出错 他们关系的属性。 EntityEntries 属性将 返回 null,因为无法将单个实体标识为源 的例外。可以进行保存时的异常处理 通过在实体类型中公开外键属性更容易。看 InnerException 了解详情。

内部错误:-

存储更新、插入或删除语句影响了意外 行数 (0)。实体可能已被修改或删除 实体已加载。看 http://go.microsoft.com/fwlink/?LinkId=472540 获取有关信息 理解和处理乐观并发异常。

【问题讨论】:

    标签: c# mysql entity-framework


    【解决方案1】:

    您收到此错误的原因是 EF 无法确定如何将您的 Product 与您的 Recipe 和 InventoryItem 关联起来。要解决此问题,请暂时删除您的 InventoryItem 并使您的产品和配方按预期保留。

    使用 EF 6 和 Core,您可以选择在父实体中定义 FK 字段并将其指定为引用的子实体的 FK。

    例如使用代码优先注解:

    public class Product
    {
       [Key]
       public Guid ProductId {get; set;}
    
       [ForeignKey("RecipeId")]
       public virtual Recipe Recipe {get; set;}
    
       public Guid RecipeId {get; set;}
    
       // ...
    }
    

    或者,您可以通过实体类型配置或覆盖 DbContext 的 OnModelCreating 使用配置来使用 .HasForeignKey() 流式方法建立外键。

    如果您不小心,以这种方式映射 FK 可能会引发问题,因为您现在有一个对 Recipe 实体的引用和一个单独的 FK 引用,并且必须确保它们保持同步。

    或者使用 EF 6,您可以为 FK 映射列,而无需在实体类型配置中使用 .Map() /w .MapKey() 或 DbContext 的 OnModelCreating 声明属性。从我读到的关于 EF Core 的内容来看,这还不是一个选择。

    一旦配方参考工作正常,InventoryItem 将是相同的。

    我在这篇文章中稍微介绍了使用引用与 FK(而不是两者): http://www.practicagility.com.au/2017/10/27/ef-a-1st-class-citizen-part-4-using-references-vs-keys/

    【讨论】:

    • 完美。感谢帮助。它使用 '[ForeignKey("RecipeId")]' 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多