How to add missing foreign key / object? Save BreezeJs with JObjects

It is a database first using entity frameworks 6 and lazy loading. I have the following device class that is missing four foreign key IDs:

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; }
}

      

Missing keys / IDs that are not generated but are automatically rendered as virtual objects:

DeviceModelId, DeviceTypeId, SolutionApplicationId, and SolutionTypeId

      

I want to save a new device using breeze, but it is looking for the device type. I tried adding deviceTypeId before saving:

newDevice.deviceTypeId = 5;

      

But it still isn't readable or saved.

[Error] Save failed: Entities in 'PortalEntities.Devices' participate in the 'FK_Devices_DeviceTypes' relationship. 0 related 'DeviceType' were found. 1 'DeviceType' is expected.

      

This is how my save statement is in my controller:

    [HttpPost]
    public SaveResult SaveChanges(JObject saveBundle)
    {
        return _repository.SaveChanges(saveBundle);
    }

      

I checked what actually happens when it tries to save. The following entity was included with the preservation. BUT the device type, which is a required field, does not exist, hence the error is its absence.

"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"
    }
  }
}

      

Since deviceTypeId didn't work, so I tried to add the device property right before saving:

newDevice.deviceType = { id:5, name: 'Blue'}; //NOTE: This already exists in the devicetype table in the database

      

But I am getting the following error:

TypeError: Cannot read property 'entityState' of undefined

      

Using Breeze, How to add this external object to keep this new device?

Edit 1: This is my DeviceType class:

    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; }
        }

      

+3


source to share


1 answer


Just add the existing object DeviceType

from your dbContext.

Something like:



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

      

0


source







All Articles