1

Let's say I have a manufacturer which produces lots of models. Each model has several attributes, and these attributes should come in a certain sort order. Here is what I did before:

  • Table "ManufacturerModel" with

    public virtual ICollection<VehicleAttribute> Attributes { get; set; }
    
  • Table "VehicleAttribute" with

    public virtual ICollection<ManufacturerModel> Models { get; set; }
    
  • and finally ModelBuilder fluent api to connect these two tables so I get a 1:n relationship:

            modelBuilder.Entity<ManufacturerModel>()
            .HasMany(mm => mm.Attributes)
            .WithMany(a => a.Models)
            .Map(mv =>
            {
                mv.ToTable("Model_VehicleAttribute", SchemaNames.Config);
                mv.MapLeftKey("ManufacturerModel_Id");
                mv.MapRightKey("VehicleAttribute_Id");
            });
    

    Works perfect. BUT: where do I put the SortOrder column? In my opinion it should be inside mapping table "Model_VehicleAttribute" so I can select a ManufacturerModel and get all Attributes in a sorted order. How can this be achieved?

Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
okieh
  • 617
  • 6
  • 17
  • possible duplicate of [EF Code First Additional column in join table for ordering purposes](http://stackoverflow.com/questions/7289160/ef-code-first-additional-column-in-join-table-for-ordering-purposes) – Yuliam Chandra Sep 26 '14 at 09:03
  • Thanks, this is a working solution. But I loose a **lot** of convenience because I can't just say "Model.Attributes.Add(...)" but must first create the Model_Attribute entity, and that for each and every Attribute I like to add to the model. Hmmm... not very satisfying, so I hoped there are other solutions. – okieh Sep 26 '14 at 10:05

0 Answers0