【问题标题】:How to add missing foreign key/entity? Save BreezeJs with JObjects如何添加缺少的外键/实体?使用 JObjects 保存 BreezeJs
【发布时间】: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


【解决方案1】:

只需从您的 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();
}

【讨论】:

  • 对不起,我的意思是使用微风,我如何添加外部实体并保存它。添加了编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-01
  • 2016-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-10
  • 2017-08-18
相关资源
最近更新 更多