Sascha Herrmann
Sascha Herrmann

Reputation: 552

Bad Navigation Property/One-to-Zero or One Relationship

this is my User Model:

public User{
   ... (no navigation Property to modeltype)

}

The following model is inspired from DocCode OrderDetails->Product where only OrderDetail has a Foreign Key to the Product. For this config i get an error message from breeze: "Bad nav properties" for Users SB and TL but not for MA.

public modeltype{
  public DateTime? ClosedBySB { get; set; }

    public long? SBId { get; set; }
    [ForeignKey("SBId")]
    public User SB { get; set; }

    public DateTime? ClosedByTL { get; set; }

    public long? TLId { get; set; }
    [ForeignKey("TLId")]
    public User TL { get; set; }

    public DateTime? ClosedByMA { get; set; }

    public long? MAId { get; set; }
    [ForeignKey("MAId")]
    public User MA { get; set; }
    ....
}

while this works:

public modeltype{
  public DateTime? ClosedBySB { get; set; }

    //public long? SBId { get; set; }
    //[ForeignKey("SBId")]
    //public User SB { get; set; }

    public DateTime? ClosedByTL { get; set; }

    //public long? TLId { get; set; }
    //[ForeignKey("TLId")]
    //public User TL { get; set; }

    public DateTime? ClosedByMA { get; set; }

    public long? MAId { get; set; }
    [ForeignKey("MAId")]
    public User MA { get; set; }
    ....
}

I think this should work? No additional Fluent Api Configuration is made. Thanks for any help.

Upvotes: 0

Views: 352

Answers (4)

Jay Traband
Jay Traband

Reputation: 17052

This bug is fixed as of breeze v 1.0.0. and thanks go to Sergey for pointing out the fix.

Upvotes: 1

Sergey Rusakov
Sergey Rusakov

Reputation: 48

I had same problem when model contained 2+ properties with same Type (1-to-1 relation). I have to deep in logic of breeze work with associations to solve this problem. It seems that breeze analyzes each of them and removes from temp array, if current association have both ends. Otherwise breeze shows 'bad nav properties' error. In case of several 1-to-1 properties with same type breeze removes one end of all this 1-to-1 relations, except first property, and show error. Try to change this strings in "addToIncompleteMap" function in breeze.js:

  incompleteTypeMap[np.entityTypeName] = assocMap;

to

if (incompleteTypeMap[np.entityTypeName]) 
  (incompleteTypeMap[np.entityTypeName])[np.associationName] = np;
else
   incompleteTypeMap[np.entityTypeName] = assocMap;

Upvotes: 1

Sascha Herrmann
Sascha Herrmann

Reputation: 552

I commented out the ForeignKey Attributes and put this in the Config file for the ModelType:

HasOptional(p => p.SB)
            .WithMany()
            .HasForeignKey(s => s.SBId)
            .WillCascadeOnDelete(false);

        HasOptional(p => p.TL)
        .WithMany()
        .HasForeignKey(s => s.TLId)
        .WillCascadeOnDelete(false);

        HasOptional(p => p.MA)
        .WithMany()
        .HasForeignKey(s => s.MAId)
        .WillCascadeOnDelete(false);

But this produces the same error. SB and TL throw bad nav property exception. The navigation property must be nullable so i used HasOptional(). I do not know where to put any other config so breeze recognizes the entityType (undefined for SB/TL therefor the exception).

Upvotes: 0

Ward
Ward

Reputation: 17863

I suspect an EF configuration problem.

You succeed when there is one navigation returning a related User entity but fail when you have three such navigation properties. You have no [InverseProperty] to help EF figure it out because you don't want navigation properties from User back to ModelType (and I can imagine why you don't want them).

I think you will have to use the Fluent API to tell EF what you mean.

Upvotes: 0

Related Questions