這是數據庫首先使用實體框架6和延遲加載。 我有下面的類器件,其缺少四個鑰匙ID:如何添加缺少的外鍵/實體?用JObjects保存BreezeJs
public partial class Device {
public int SolutionId { get; set; }
public string SiteId { get; set; }
public string Name { get; set; }
public int SysId { get; set; }
public Nullable<int> SysType { get; set; }
public string SerialNumber { get; set; }
public Nullable<int> ParentId { get; set; }
public virtual DeviceModel DeviceModel { get; set; }
public virtual DeviceType DeviceType { get; set; }
public virtual SolutionApplication SolutionApplication { get; set; }
public virtual SolutionType SolutionType { get; set; }
}
丟失的鑰匙/未產生,但是自動IDS得到映射爲虛擬對象:
DeviceModelId, DeviceTypeId, SolutionApplicationId, and SolutionTypeId
我要救使用微風的新設備,但它正在尋找設備類型。我嘗試在保存之前添加deviceTypeId:
newDevice.deviceTypeId = 5;
但是它仍然沒有讀取,也沒有保存。
[Error] Save failed: Entities in 'PortalEntities.Devices' participate in the 'FK_Devices_DeviceTypes' relationship. 0 related 'DeviceType' were found. 1 'DeviceType' is expected.
這是我如何保存聲明在我的微風控制器:
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
return _repository.SaveChanges(saveBundle);
}
我檢查實際獲得通過什麼,因爲它試圖做一個拯救。捆綁保存包含以下實體。但是所需字段的設備類型不存在,因此缺少它的錯誤。
"entities": [
{
"SolutionId": -1,
"SiteId": "11111d2",
"Name": "asdf",
"SysId": 0,
"SysType": null,
"SerialNumber": null,
"ParentId": null,
"entityAspect": {
"entityTypeName": "Device:#HtPortal.Data.Portal",
"defaultResourceName": "Devices",
"entityState": "Added",
"originalValuesMap": {},
"autoGeneratedKey": {
"propertyName": "SolutionId",
"autoGeneratedKeyType": "Identity"
}
}
}
由於deviceTypeId沒有工作,所以我想正確的保存之前添加設備類型:
newDevice.deviceType = { id:5, name: 'Blue'}; //NOTE: This already exists in the devicetype table in the database
,但我得到了以下錯誤:
TypeError: Cannot read property 'entityState' of undefined
使用微風,如何添加這個外國實體,以便我可以保存這個新設備?
編輯1:這是我的設備類型類別:
public partial class DeviceType {
public DeviceType() {
this.Devices = new HashSet<Device>();
}
public byte Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Device> Devices { get; set; }
}
您是否嘗試過'PascalCase',到屬性相匹配的實體,即'newDevice.DeviceType = {ID:5}'? - 或者應該是'{DeviceTypeId:5}'如果'DeviceType'遵循你的PK命名約定 – StuartLC 2014-11-03 19:11:20
是的,我試過'PascalCase'。它沒有任何區別。 「 – RedApple 2014-11-03 20:39:16
」缺少四個外鍵ID「。這聽起來像是一個既成事實。但爲什麼你不加它們呢? – 2014-11-03 21:16:38