locked
uncaught exception: Parent entity is not in the parent entity set for this association. RRS feed

  • Question

  • User299698256 posted

    I am getting this uncaught exception: Parent entity is not in the parent entity set for this association. using upshotjs. I think the problem is that even though i declare my data model is not matching or binded to the metadata model.

    I have something like this in my ViewModel

    UserModel ={

    CountryModel

    LanguageModel

    }

            var dataSourceOptions = {
                providerParameters: { url: options.serviceUrl, operationName: "GetUsers" },
                entityType: entityType,
                bufferChanges: true,
                mapping: MyApp.User
            };
            var editorDataSource = new upshot.RemoteDataSource(dataSourceOptions);

            var countriesDataSource = new upshot.RemoteDataSource({
                providerParameters: { url: options.serviceUrl, operationName: "GetCountryOptionsForUser", operationParameters: parentEntitiesOperationParameters },
                entityType: "Country:#MyApp.Core.Entities",
                dataContext: editorDataSource.getDataContext()
            });

    the countriesDataSource gets all the countries and languages.

    CountryModel={

    Name

    Languages <- Array of Language Models

    }

    If I supply the dataContext option then this is not a problem but I have another model inside the Country Model call Languages and this Language model is not binded or mapped to the one supplyed by the metadata model. How can I bind the raw data to the DataSource? I try using the editorDataSource.getDataContext().addMapping(MyApp.Language.Type, MyApp.Language) but this does not seem to work.

    Monday, April 23, 2012 11:38 PM

All replies

  • User1364817430 posted

    The mapping must represent the data you get back from the service. If you retrieve your data from the server it's always an IQueryable of a certain type. The complete model of this type must recreated in a JS object, then this object can be mapped an represent your object(s) you get back from the server. In your case i think you have only mapped one of the child objects.

    greetings

    Kristof

    Tuesday, April 24, 2012 2:30 AM
  • User299698256 posted

    could you please explain how to map my child objects. As I understand when you fetch data using RemoteDataSource and if you supply the mapping model then I thought it would automatically mapped all the child objects as well.

    Tuesday, April 24, 2012 4:02 AM
  • User1364817430 posted

    Hi,

    You can do this as follow:

    function TodoItem(properties) {
       var self = this;
       properties = properties || {};
       self.TodoItemId = ko.observable(properties.TodoItemId || 0);
       self.Title = ko.observable(properties.Title);
       self.IsDone = ko.observable(properties.IsDone);
       // Added for updating entities.
       upshot.map(properties, "TodoItem:#Ria4HTMLWithoutSPA.Models", self); // Add properties from the server. 
       upshot.addEntityProperties(self); // add properties managed by upshot
    };

    For more info you can take a look at my sample application http://ria4htmldemo.codeplex.com/

    Greetings

    Kristof

     

    Tuesday, April 24, 2012 4:34 AM
  • User299698256 posted

    yes i understand that part. The problem here and i think is a bug or something is that when you get the Json from the server it gets a __type: property with upshot and i think upshot need this __type to bind the model but the problem is that if you have class as below:

    [DataContract(Name = "Country")]
    
    public class CountryViewModel()
    
    {
    
    [Key]
    
    [DataMember]
    
    public int Id {get;set;}
    
    [DataMember]
    
    public string Name {get;set;}
    
    [DataMember]
    
    public IEnumerable<Language> Languages {get;set;}
    
    }
    
    [DataContract(Name = "Language")]
    
    public class LanguageViewModel()
    
    {
    
    [Key]
    
    [DataMember]
    
    public int Id {get;set;}
    
    [DataMember]
    
    public string Name {get;set;}
    
    }

    If you do a remotedataaccess then this would fetch a json data something like this :

    {"Results":[
    {"__type":"Country:MyApp.Core.Entities",
    "Id":1,
    "Languages":[{"Id":1,"Name":"English"
    },
    {"Id":2,"Name":"Spanish"}],"Name":"USA"}
    }],
    "TotalCount":1}


    This data when binded, upshot only knows the one with the __type Model and but for the Language model it does not have the __type so it does not bind properly. How can I get the __type on the Languages property?

    Tuesday, April 24, 2012 7:39 AM
  • User1364817430 posted

    I think the problem you are struggling with is:

    you need to provide the metadata of your objects. If you use Steven Sandersons demo, you can use the following line of code:

    @(Html.UpshotContext(bufferChanges: true).DataSource<DataServiceController>(x => x.MethodToRetrieveData())) 
    

    This will configure the metadata u need and configure upshot for you.

    Take a look what it generates, and then you can use it manually if you want to.

    greetings

    Kristof

    Tuesday, April 24, 2012 8:20 AM