Reputation: 762
I am using Entity Framework Code First approach to have Data Access in my application. I have a table Web_Documents
and a table Web_Profils
and a relation table Web_Profil_joint_Documents
, all of which I have made POCOs for. My problem is when I added a migration, Entity Framework created an extra relation's table.Now in my context I make a binding between both as follows:
modelBuilder.Entity<Web_Profils>()
.HasMany<Web_Documents>(p => p.Documents)
.WithMany(d => d.Profils)
.Map(Web_Profil_joint_Documents =>
{
Web_Profil_joint_Documents.MapLeftKey("IDProfil");
Web_Profil_joint_Documents.MapRightKey("IDDocument");
Web_Profil_joint_Documents.ToTable("Web_Profil_joint_Documents");
});
But I think this adds a new table, and since I already have Web_Profil_joint_Documents
it creates Web_Profil_joint_Documents1
and adding data to the first, actually fills the second. I have tried reverse engineer the tables to my model but that just created a whole mess. I've also tried deleting the tables in SQL Server Management and then manually modifying migrations to have Web_Profil_joint_Documents
only but had error everytime.
Does this code create a new table? I thought it would add a many to many relationship between both tables and add it to the already existing relations table.
Any ideas how to fix this?
Upvotes: 1
Views: 291
Reputation: 6766
You should not define related entity POCO class for many to many relationship. Entity framework does generates it automatically for us. From the comment I come to know that why you had define the relationship entity.
Related entity will contain two columns.
Web_Profils
Web_Documents
.So far everything is simple. Now you wanted to access the data in relationship table (which is very very very rare) then you can do something like this.
suppose you have instance of web_profile and you wanted to get its record in relationship then first column is id (primary key) of instance and second you can get it by selecting id of its related collection of Web_Profil_joint_Documents
.
Web_Profils.Web_Profil_joint_Documents.Select(d => d.Id)
so in that way you also achieve second column of relationship table.
Upvotes: 3