Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have Many to Many Association in Entity Framework Code First

I am just getting started with EF and I watched some great tutorial videos. I am stuck with the following.

I have a class for a collection of files, I would like these to be tied to events and/or people

public class file{     public int id {get;set;}     public string path {get;set;} }  public event {     public int id {get;set;}     public string eventname {get;set}     public virtual ICollection<file> files {get;set;}     public event(){ files = new list<file>();} }  public person {     public int id {get;set;}     public string name {get;set}     public virtual ICollection<file> files {get;set;}     public person(){ files = new list<file>();} } 

Now when I generate the database my file table has a PersonID AND EventID.

I want to be able to let users attach files to people and/or events.

like image 287
Code Noob Avatar asked Nov 23 '10 01:11

Code Noob


People also ask

How do I create a many-to-many relationship in Entity Framework?

To configure many-to-many relationship Using Data Annotations, you need to create the Join Table in the model. The Join Table BookCategory will have properties for the primary key of both the table. It will have two navigational properties one each for Book and Category class.

What is many-to-many relationships in code first approach?

A many-to-many relationship is defined in code by the inclusion of collection properties in each of the entities - The Categories property in the Book class, and the Books property in the Category class: public class Book. { public int BookId { get; set; }

How do I map a one-to-many relationship in Entity Framework?

“HasMany” and “WithMany” method is used to define one-to-many or many-to-many relation in entity framework. We can configure one-to-many relationships between People and PeopleAddress using Fluent API by the following code in the model class: protected override void OnModelCreating(DbModelBuildermodelBuilder) {

How do I code first in Entity Framework?

Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…


1 Answers

What you are getting is the default behavior of EF Code First in terms of mapping a 1 to Many association to the database. For example you have a ICollection<File> on Person class, as a result, EF will create a FK on Files table (PersonId) and map it to Id PK on Persons table.

Now, my guess is that you like to have a many to many relationship between File and Person, so that each file can relates to many Persons and each Person can have many files (same story for Event object as well). One way to achieve this is to put navigation properties on File class pointing to Event and Person classes. So, your model should be changed to this:

public class File {     public int Id { get; set; }     public string Path { get; set; }     public virtual ICollection<Event> Events { get; set; }     public virtual ICollection<Person> Persons { get; set; } }  public class Event {     public int Id { get; set; }     public string EventName { get; set; }     public virtual ICollection<File> Files {get;set;} }  public class Person {     public int Id { get; set; }     public string Name { get; set; }     public virtual ICollection<File> Files { get; set; } }  public class MyContext : DbContext {     public DbSet<Person> Persons { get; set; }     public DbSet<Event> Events { get; set; }     public DbSet<File> Files { get; set; } } 

As a result, EF will create link tables (Events_Files and Files_Persons) to map these many to many associations to the database.

Update:
When using POCOs with EF, if you mark your navigation properties as virtual you will opt-in to some of the additional EF supports like Lazy Loading and Relationship Fixup. So in general have a virtual keyword in navigation properties considered to be a good practice.

like image 178
Morteza Manavi Avatar answered Sep 23 '22 07:09

Morteza Manavi