0

I'm using entity framework code first approach

I have a class

public class Movie
{
    public int Id { get; set; }

    public string Title { get; set; }

    public Person Director { get; set; }

    public virtual ICollection<Person> Actors { get; set; }
}

and a class

public class Person
{
    public int Id { get; set; }

    public string Name { get; set; }
}

When the database is created I get one table Movies with Id, Title, Director_Id and a table Person with Id and Name. I expect to have a table Movies_Persons with columns Movie_Id and Actor_Id

How can I achieve this?

Mark Titorenkov
  • 91
  • 2
  • 13

1 Answers1

1

Your Problem is, that you don`t tell the Person Class, that there can be multiple Movies per person.

So by adding the following line in your person class:

public virtual ICollection<Movie> Movies { get; set; }

Your entity knows that both your classes can have multiple references to the other class. To fulfill this requirement Entity Framework will create a third table with Movie_ID and Person_ID.

If you want more informations just look for: Entity Framework - Many to many relationship

or follow this link: http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

You can check out the other articels on that page too, if you are new to entity framework.

UPDATE: Sorry i missed, that you are already have another reference to your person table. Here you have to tell your entity framework, which way you want to reference the two tables by fluent api. Check out this stackoverflow answer. That should do the trick. You have to insert this code into your OnModelCreating Function of your DbContext Class.

So your final code should look like this:

public class Movie
{
    public int Id { get; set; }

    public string Title { get; set; }

    public virtual Person Director { get; set; }

    public virtual ICollection<Person> Actors { get; set; }
}

public class Person
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Movie> Movies_Actors { get; set; }

   public virtual ICollection<Movie> Movies_Directors { get; set; }
}

And in your OnModelCreating add following code:

modelBuilder.Entity<Movie>()
            .HasMany(a => a.Actors)
            .WithMany(a => a.Movies_Actors)
            .Map(x =>
            {
                x.MapLeftKey("Movie_ID");
                x.MapRightKey("Person_ID");
                x.ToTable("Movie_Actor");
            });

modelBuilder.Entity<Movie>()
.HasRequired<Person>(s => s.Director)
.WithMany(s => s.Movies_Directors);

I don't have the possibility to test the code, but that should do the trick. If you have to do some adjustments to make it work, plz add them in the comments, so other ppl can benefit from it.

Rey
  • 3,663
  • 3
  • 32
  • 55
BabbleGum
  • 66
  • 6
  • It doesn't work. It just creates a column Movie_Id in Person. If I remove Director then I get a middle table but it doesn't work when the Director property is there. – Mark Titorenkov Jun 11 '17 at 16:04