1

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) {

                         });
halfer
  • 19,824
  • 17
  • 99
  • 186
Leroy Meijer
  • 1,169
  • 17
  • 40

3 Answers3

0

I think we need more information. Is the 'Owner' property an instance of another entity or is it a primitive type, i.e. string, number etc?

If it is an entity then I would first check that your 'newOwner' variable was also in fact an entity.

Jay Traband
  • 17,053
  • 1
  • 23
  • 44
0

If owner is an observable, you need to asign the new value as: owner(newOwner).

Greetings.

Julián Yuste
  • 1,472
  • 10
  • 22
  • This is what I also tried `Container().Owner(newValue);` But it didn't worked – Leroy Meijer May 28 '13 at 11:57
  • Looking your exception the problem is that the entities are not from the same entitymanager. You can always try to assign OwnerID insteadof Owner: Container().OwnerID(newValue.OwnerID()). – Julián Yuste May 28 '13 at 12:11
  • I see, but I need the new Owners data immediatly for displaying. But I don't understand how I can have 2 different entitymanager. I have 1 dataservice object that takes care of everything ;S – Leroy Meijer May 28 '13 at 12:17
0

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.

Brandon S
  • 1,543
  • 1
  • 13
  • 13
  • I don't understand because I don't want to create a new owner I already have this owner! I just want to bind it to a Car. So I have a list of owners, choose one of them from a dropdownlist and the selected owner is put in the property car().Owner() – Leroy Meijer May 29 '13 at 09:03
  • Gotcha. Is your `dataservice` object creating a new `manager` object during this workflow? Is the `dataservice` object a global variable? – Brandon S May 30 '13 at 02:23
  • I will check this tomorrow when I get back at work, you could probably be right because in each javascript file I do: `var dataservice = require('services/dataservice');` //in my model `dataservice = new dataservice('api/data'); ` I believe this is the problem indeed, I need to make my dataservice a Singleton. Or do you have a better solution? How to make this global via one file? I will this tomorrow ;) – Leroy Meijer May 30 '13 at 16:49
  • Yes that was it exactly ;) I created a singleton of my Dataservice and it's working properly now;) I accomplished this with: [link]http://stackoverflow.com/questions/1479319/simplest-cleanest-way-to-implement-singleton-in-javascript – Leroy Meijer May 31 '13 at 12:30
  • Of course I forgot to thank you :) Thank you very much for pointing this out ;) – Leroy Meijer May 31 '13 at 12:36