我无法添加InventoryItem类型的新实例,因为ChangeTracker认为我修改了主键。但它就像构造函数一样。
另一方面,我可以毫无问题地添加项目。但是当我在创建InventoryItemType(即InventoryItem的NavigationProperty)之后插入它时,它就失败了。
我搜索,尝试,更新EF从5到6.1.1并且我投降了...我需要你的帮助
InventoryItemType iit = new InventoryItemType
{
InventoryFamilyID = 12,
Name = checkInPackage.Name,
Provider = shippingDestination.RelatedWarehouse.Name,
Reference = "Paquete nº " + checkInPackage.CheckInPackageId
};
this.inventoryItemTypeRepository.InsertOrUpdate(iit);
this.inventoryItemTypeRepository.Save();
InventoryItem ii = new InventoryItem
{
WarehouseID = checkInPackage.WarehouseID,
Price = 0,
InventoryItemTypeID = iit.InventoryItemTypeID
};
this.inventoryItemRepository.InsertOrUpdate(ii); // th
this.inventoryItemRepository.Save();
存储库有:
public void InsertOrUpdate(InventoryItem inventoryitem)
{
if (inventoryitem.InventoryItemID == default(int)) {
// New entity
context.InventoryItems.Add(inventoryitem);
} else {
// Existing entity
context.Entry(inventoryitem).State = EntityState.Modified;
}
}
使用FIX编辑:
我对关系InventoryItemType
1到InventoryItem
n
在InventoryItemType.cs中执行此更改后,它运行时没有错误:
public InventoryItemType()
{
InventoryItems = new HashSet<InventoryItem>();
}
public virtual ICollection<InventoryItem> InventoryItems { get; set; }
在业务逻辑方面
iit.InventoryItems.Add(
new InventoryItem
{
WarehouseID = checkInPackage.WarehouseID,
Price = 0,
InventoryItemType = iit
}
);
this.inventoryItemTypeRepository.InsertOrUpdate(iit);
this.inventoryItemTypeRepository.Save();
答案 0 :(得分:1)
根据this description和this one,不是在InventoryItemTypeID
实体中设置InventoryItemType
设置InventoryItem
,也许可行。
InventoryItem ii = new InventoryItem
{
WarehouseID = checkInPackage.WarehouseID,
Price = 0,
InventoryItemType = iit
};