javascriptbreeze

Assign new values in a Breeze entity


I'm developing a spa web application with BreezeJS and the DurandalJS Framework. I came accross a problem which I can't fix.

I have a entity called: Car, this entity contains name, number, owner, type en manufacturer. In this entity the name and number are filled in as the entity is created AND saved in the database. The other properties are allowed to be NULL.

This because the other values are filled in during a modal/ dialog screen. Here a user can select a owner from a list and also a type and manufacturer from a list. When the user selects one from a dropdown the selected value should be assigned to the value of the Car entity. How can I get this to work?

    Car().Owner = newOwner;
Car.Owner() = newOwner;

This won't work. I tried a lot of combinations. Remember that the value was null first and that I can't insert a new value;S

Edit 1

Here the Entity Framework model of Car

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Estimate_App.Models
{
    public class tblCar
    {
        [Key]
        public int CarID { get; set; }
        public string CarNumber { get; set; }
     
        private DateTime _CreationDate;
        public DateTime CreationDate
        {
            get { return this._CreationDate; }
            set { this._CreationDate = DateTime.Now; }
        }

        //This is the Owner
        [ForeignKey("Owner")]
        public int? OwnerID { get; set; }
        public tblOwner Owner { get; set; }
    }
}

Here is what I put in my Car().Owner(), consider Container should be Car (this is an other project with the same problem)

http://s13.postimg.org/pfvfdkeqv/question_with_entitys.png

I hover my mouse over newValue.

Edit 2

By a answer of Julián Yuste I also tried this but it didn't work. Here is the error:

Error message

When I do Container().Owner(newValue);

Edit 3

The code that fetches the owners

breeze.EntityQuery.from("Customers")
                         .using(dataservice.manager)
                         .execute().then(function (data) {
                             data.results.forEach(function (item) {
                                 tempCustomerList.push(item); //ko.observableArray([]);
                             });
                         }).fail(function (data) {

                         });

Solution

  • Are you using the EntityManager from your dataservice object in order to create the newOwner object?

    In other words, you probably shouldn't be doing this*:

    var newOwner = new Owner();
    newOwner.OwnerID = 123;
    

    You should do this:

    var newOwner = dataservice.manager.createEntity('Owner', { OwnerID: 123 });
    

    *Note that can actually use new Owner(), but that requires you to define entity constructors in your code. :-)

    For more information, check out the Breeze documentation: http://www.breezejs.com/documentation/creating-entities

    Also, note that you can read the Breeze JavaScript code to help you understand the issue. If you search breeze.debug.js for your error message (An Entity cannot be attached to an entity in another EntityManager. One of the two entities must be detached first.), you will find the line of code that is causing the exception. It may be helpful to backtrack from there.

    Edit

    The answer to the question was to make sure that the EntityManager object is a Singleton (in this case, the dataservices.manager object).

    In other words, use the same EntityManager object to update objects as you use to query for objects.