这是数据库首先使用实体框架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