Brock Noland
Brock Noland

Reputation: 287

Breeze.js 1.4.1 properties undefined not null

I upgraded to 1.4.1 per the recommendation of Breeze support but I am having the following issue. Previously, the navigation properties on newly created entities where defined, but null valued knockout observables. I modified the Breezejs TODO application to show this.

My data model is below, and my front end code is here:

function reproduce() {
  breeze.NamingConvention.camelCase.setAsDefault();
  var manager = new breeze.EntityManager(serviceName);
  manager.fetchMetadata().then(function () {
    var parent = manager.createEntity('Parent');
    console.log('otherProperty ' + parent.otherProperty());
    console.log('childOne ' + parent.childOne());
    // I cannot call parent.childrenTwo() since childrenTwois undefined
    console.log('childrenTwo ' + parent.childrenTwo);
  });
}

The issue is that in previous versions of breeze, the properties otherProperty and childOne would be a knockout observable with a null value and the property childrenTwo would be an empty observable array. However, as I see in the console all three properties are undefined? Is this intentional?

I could of course define them myself but that is a lot of work and something I expect breeze todo for me. Also according to the Breeze docs "There is rarely reason to define properties that are already described in metadata." http://www.breezejs.com/documentation/extending-entities

Update 1:

Thanks to Jay Traband, in my reproduction app I was not setting casing correctly. However childrenTwo is still undefined and I believe it should be an observable array. My production app does set casing so I'll have to re-investigate that.

Update 2:

Thanks again to Jay Traband, I found that the breeze metastore does not know about the ChildTwo type. Therefore it seems I am not registering it somehow? I am much more familiar with Java Hibernate than Entity Framework. Is something missing from my data model below?

Updated 3:

ChildTwo didn't have an explicit foreign key, I added that and it worked. I guess I really need to take to heart that Breeze wants an explicit foreign key.

public class ChildTwo
{
  [Key]
  public int Id { get; set; }

  public int ParentId { get; set; }

  [ForeignKey("ParentId")]
  public Parent Parent { get; set; }
}

Data model.

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Todo.Models
{
  public class Parent
  {
    public Parent()
    {
      ChildrenTwo = new List<ChildTwo>();
    }
    [Key]
    public int Id { get; set; }

    [Required]
    public string OtherProperty { get; set; }

    [Required]
    public ChildOne ChildOne { get; set; }

    [Required]
    public IList<ChildTwo> ChildrenTwo { get; set; }
  }
  public class ChildOne
  {
    [Key]
    [ForeignKey("Parent")]
    public int Id { get; set; }

    public Parent Parent { get; set; }
  }
  public class ChildTwo
  {
    [Key]
    public int Id { get; set; }

    public Parent Parent { get; set; }
  }
} 

Upvotes: 1

Views: 605

Answers (1)

Jay Traband
Jay Traband

Reputation: 17052

I just did some simple tests and was unable to repro this. I see navigation properties for my entities as knockout observables in all of my tests after calling createEntity. A couple of ideas;

Are you sure that you aren't inadvertantly

  • using the backingStore or backbone model library instead of knockout. via breeze.config.initializeAdapter.
  • applying a different casing to your properties, i.e. via use of breeze.NamingConvention.

Upvotes: 1

Related Questions