dafriskymonkey
dafriskymonkey

Reputation: 2189

BreezeJs : many to many relationship

My entities are 'Books' & 'Categories'. Each book can have multiple categories, and each category can be related to several books.

To implement this I made the models like this :

public class Book
{
    [Key]        
    [DataMember]
    public Guid BookId { get; set; }

    public virtual ICollection<Category> Categories { get; set; }
}

public class Category
{
    [Key]
    [DataMember]
    public Guid CategoryId { get; set; }
}

My DbContext is like this :

// Some code

public DbSet<Book> Books { get; set; }
public DbSet<Category> Categories { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Book>()
        .HasMany(c => c.Categories)
        .WithMany()
        .Map(x =>
        {
            x.MapLeftKey("BookId");
            x.MapRightKey("CategoryId");
            x.ToTable("BookCategoryMapping");
         });

 }

// Some code

At the client side, books entities doesnt contain any categories key ???

Im wondering if my model is correct.

the Table BookCategoryMapping has been created but it seems that i cant populate it with data using breeze.

How can I, using breeze, create a book entity and push categories into it ?

Thanks.

Upvotes: 0

Views: 273

Answers (1)

Steve Schmitt
Steve Schmitt

Reputation: 3209

On the server side, EF can create a relationship between Book and Category by adding a row directly to the BookCategoryMapping table. Breeze on the client cannot do that. It can only create the relationship by creating a BookCategoryMapping entity, which will then be sent to the server when you call saveChanges().

You will need to model BookCategoryMapping as an entity.

Upvotes: 1

Related Questions