entity framework 4.1 - EF Code First Additional column in join table for ordering purposes -
I have two bodies for which I am creating an included table
Public Class Student {Public Ent ID (Received); Set; } Public string name {get; Set; } Public virtual iconging & lt; Image & gt; Images {get; Set; }} Set the image of the public square {Public Entity}; } The name of the public string file {get; Set; } Public virtual iconging & lt; Students & gt; Student {Received; Set; }} Secure Override Zero OnModelCreating (DbModelbuilder Model Builder) {modelBuilder.Entity & lt; Students & gt; () .HasMany (i => i.Iages) .Many (s = & gt; s.Students) .map (m = & gt; m ToTable ("StudentImages")); } I would like to add an extra column to allow the chronological order of student image.
Where do I want to insert the relevant code?
Do you want to use that new column in your application? In such a case you can not do this with your model Many-to-many relations work only when there is nothing except the foreign key from the main table in the junction table. Once you add additional columns in contact with your application, then the junction table is in the form of another object = you need a third class. Your model should look like this:
Public class student representation {public input student ID} set; } Get a Public Offering Image ID { Set; } Public indefinite order {receive; Set; } Public Virtual Student Student {Received; Set; } Public virtual image image {get; Set; }} Public class students {public id (get; set;} public string name {get; set;} public virtual iconing & lt; student image & gt; images {get; set;}} public class image {public Ent Id} set;} Public string filename {get; set;} Public virtual iconing & lt; student image & gt; student {received; set;}} and also your mapping Change should:
Protected Override Zero OnModelCreating (DbModelbui Lder modelBuilder) {modelBuilder.Entity & lt; StudentImages & gt; (). HasKey (C => New {si.StudentId, si.ImageId}); // No rest should be needed - by the conventions model Builder .Untiti () .maswa (s = & gt; s.Images) with compulsory (c => C. student) .HasForeignKey (C = & gt; si .StudentId; Modelbuilder.Entity () .HasMany (s = & gt; s.Students) with compulsory (C => si.Image) .HasForeignKey (C = & gt; Si.IgeId);}
Comments
Post a Comment