Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何添加缺少的外键/实体?使用作业对象保存BreezeJs_Angularjs_Foreign Keys_Breeze_Foreign Key Relationship - Fatal编程技术网

Angularjs 如何添加缺少的外键/实体?使用作业对象保存BreezeJs

Angularjs 如何添加缺少的外键/实体?使用作业对象保存BreezeJs,angularjs,foreign-keys,breeze,foreign-key-relationship,Angularjs,Foreign Keys,Breeze,Foreign Key Relationship,这是首次使用实体框架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&g

这是首次使用实体框架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; }
}
我想使用breeze保存新设备,但它正在查找设备类型。在保存之前,我尝试添加deviceTypeId:

newDevice.deviceTypeId = 5;
newDevice.deviceType = { id:5, name: 'Blue'}; //NOTE: This already exists in the devicetype table in the database
但它仍然无法读取和保存

[Error] Save failed: Entities in 'PortalEntities.Devices' participate in the 'FK_Devices_DeviceTypes' relationship. 0 related 'DeviceType' were found. 1 'DeviceType' is expected.
以下是我在breeze控制器中保存语句的方式:

    [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"
    }
  }
}
由于deviceType ID不起作用,因此我在保存之前尝试添加deviceType:

newDevice.deviceTypeId = 5;
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:这是我的设备类型类:

    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; }
        }
公共部分类设备类型{
公共设备类型(){
this.Devices=new HashSet();
}
公共字节Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection设备{get;set;}
}

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

您是否尝试了
PascalCase
,以匹配实体上的属性,即
newDevice.DeviceType={id:5}
或者应该是
{DeviceTypeId:5}
如果
DeviceType
遵循您的PK命名约定,我已经尝试了
PascalCase
。这没什么区别,“缺少四个外键ID”。这听起来像是既成事实。但是为什么不添加它们呢?我不想添加它们,因为每次我更新EDMX时,它都会被删除。如果我将它添加到一个单独的分部类,它在保存时不会拾取它。对不起,我的意思是使用breeze,如何添加外部实体并保存它。添加了编辑。