【发布时间】:2014-11-03 18:50:27
【问题描述】:
这是数据库首先使用实体框架 6 和延迟加载。 我有以下类型的设备缺少四个外键 ID:
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; }
}
缺少未生成但自动映射为虚拟对象的键/ID:
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 不起作用,所以我尝试在保存之前添加 deviceType:
newDevice.deviceType = { id:5, name: 'Blue'}; //NOTE: This already exists in the devicetype table in the database
但我收到以下错误:
TypeError: Cannot read property 'entityState' of undefined
使用 Breeze,我如何添加这个外部实体以便我可以保存这个新设备?
编辑 1:这是我的 DeviceType 类:
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}? - 或者如果DeviceType遵循您的 PK 命名约定,则应该是{ DeviceTypeId:5} -
是的,我试过
PascalCase。没什么区别。 -
“缺少四个外键 ID”。这听起来像是既成事实。但是你为什么不直接添加它们呢?
-
我不想添加它们,因为每次我更新 EDMX 都会被删除。如果我将它添加到单独的部分类中,它在保存时不会拾取它。
标签: angularjs foreign-keys breeze foreign-key-relationship