【发布时间】: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