I have a table in my db (a many to many table) that two classes A and B have created like this.
this.HasMany(t => t.TrafficImageQuestions)
.WithMany(t => t.TrafficImages)
.Map(m =>
{
m.ToTable("TrafficImage_Answers");
m.MapLeftKey("TrafficImagesGuid");
m.MapRightKey("TrafficImageQuestionsId");
});
Now i would like to assosiate my custom class to this same table “TrafficImage_Answers”, the class offcause have the left and right key and then also a 3. custom property.
(i did add the column to the database “Answer”)
public class TrafficImageAnswer
{
public System.Guid TrafficImageGuid { get; set; }
public int TrafficImageQuestionId { get; set; }
public byte Answer { get; set; }
}
I am doing this as i want entity model to keep track of my many to many relationship of A and B and still be able to look up the 3. property Answer that is in the database.
What i have tried
I tried to do the following:
this.Property(t => t.TrafficImageQuestionId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.TrafficImageGuid)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
// Table & Column Mappings
this.ToTable("TrafficImage_Answers");
But i get that the table already exists, logic. I need to tell it that it just should use that table and not try to create it.
(im doing this with DB mitigrations in EF 5 and Package manager).
That is not supported. If you want to have additional field in the junction table for many-to-many relation you cannot map it as many-to-many any more. Each table can be mapped only once but mapping table to entity and to many-to-many relation in the same time makes it mapped twice.
You must change your
TrafficImageQuestionsandTrafficImagesto use one to many relations withTrafficImageAnswerinstead of many-to-many relation with each other.