2014-11-03 63 views
3

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

您是否尝试过'PascalCase',到属性相匹配的实体,即'newDevice.DeviceType = {ID:5}'? - 或者应该是'{DeviceTypeId:5}'如果'DeviceType'遵循你的PK命名约定 – StuartLC 2014-11-03 19:11:20

+0

是的,我试过'PascalCase'。它没有任何区别。 “ – RedApple 2014-11-03 20:39:16

+0

”缺少四个外键ID“。这听起来像是一个既成事实。但为什么你不加它们呢? – 2014-11-03 21:16:38

回答

0

刚刚从你的DbContext添加现有DeviceType对象。

喜欢的东西:

using (YourDbEntities context = new YourDbEntities()) 
{ 
    Device newDevice = new Device(); 

    //whatever initialisation you need do.... 

    newDevice.DeviceType = context.DeviceTypes.Find(5) 

    // the above line assumes that id is PK for DeviceType and you want the entry 
    // with id==5 - if not, other ways such as DeviceTypes.Where(d=>d.Id==5).First() 
    // to find the object in your existing db will work too! 

    context.Devices.Add(newDevice); 
    context.SaveChanges(); 
} 
+1

对不起,我的意思是使用微风,我如何添加外国实体并保存它。添加了编辑。 – RedApple 2014-11-04 14:32:41