Search code examples
c#entity-frameworkef-code-first

Make mapping relationship to three table using EF CodeFirst


I am student developer at C#. I have a question while making relationship to 3 tables. My tables are like this.

 public class ImdbContext : DbContext
    {
        public DbSet<Cast> Cast { get; set; }
        public DbSet<CastRole> CastRole { get; set; }
        public DbSet<Movie> Movie { get; set; }
        public DbSet<MovieCastRoleMapping> MovieCastRoleMapping { get; set; }

        public ImdbContext() : base("ImdbDbContext")
        {
        }
    }

    [Table("Movies")]
    public class Movie
    {
        [Key]
        public int movieId { get; set; }
        public string movieName { get; set; }
        public string movieYear { get; set; }
        public string movieLink { get; set; }
        public string movieImageUrl { get; set; }
        public virtual MovieCastRoleMapping MovieCastRoleMapping { get; set; }
    }
    [Table("CastRoles")]
    public class CastRole
    {
        [Key]
        public int castRoleId { get; set; }
        public string castRoleName { get; set; }

        public virtual MovieCastRoleMapping MovieCastRoleMapping { get; set; }
    }
    [Table("Casts")]
    public class Cast
    {
        [Key]
        public int castId { get; set; }
        public string nameSurname { get; set; }
        public string biography   { get; set; }

        public virtual MovieCastRoleMapping MovieCastRoleMapping { get; set; }

    }

I did not find a clear solution to make correct relation for 3 tables. When I tried to make it myself, I facing an eror like this:

The ForeignKeyAttribute on property 'fkCastId' on type 'ImdbEntity.Models.MovieCastRoleMapping' is not valid. The navigation property 'castId' was not found on the dependent type 'ImdbEntity.Models.MovieCastRoleMapping'. The Name value should be a valid navigation property name.

I know I need to set navigation property. Untill now, I was using EF DbFirst but I want to make it EF CodeFirst without using a diagram. Could you help me for fallowing class correctly ?

[Table("MovieCastRoleMappings")]
public class MovieCastRoleMapping
{
    [ForeignKey("castId")]
    public int fkCastId { get; set; }
    [ForeignKey("castRoleId")]
    public int fkCastRoleId { get; set; }
    [ForeignKey("movieId")]
    public int fkMovieId { get; set; }
}

Solution

  • public class ImdbContext : DbContext
    {
        public DbSet<Cast> Cast { get; set; }
        public DbSet<CastRole> CastRole { get; set; }
        public DbSet<Movie> Movie { get; set; }
        public DbSet<MovieCastRoleMapping> MovieCastRoleMapping { get; set; }
    
        public ImdbContext() : base("ImdbDbContext")
        {
        }
    }
    
    [Table("Movies")]
    public class Movie
    {
        [Key]
        public int movieId { get; set; }
        public string movieName { get; set; }
        public string movieYear { get; set; }
        public string movieLink { get; set; }
        public string movieImageUrl { get; set; }
    }
    
    [Table("CastRoles")]
        public class CastRole
        {
            [Key]
            public int castRoleId { get; set; }
            public string castRoleName { get; set; }   
        }
    
    [Table("Casts")]
    public class Cast
    {
        [Key]
        public int castId { get; set; }
        public string nameSurname { get; set; }
        public string biography   { get; set; }
    }
    
    [Table("MovieCastRoleMappings")]
    public class MovieCastRoleMapping
    {
        [Key, Column(Order = 1)]
        public int fkCastId { get; set; }
        [ForeignKey("fkCastId")]
        public virtual Cast Cast { get; set; }
    
        [Key, Column(Order = 2)]
        public int fkCastRoleId { get; set; }
        [ForeignKey("fkCastRoleId")]
        public virtual CastRole CastRole { get; set; }
    
        [Key, Column(Order = 3)]
        public int fkMovieId { get; set; }
        [ForeignKey("fkMovieId")]
        public virtual Movie Movie { get; set; }
    }
    

    That's for the solution. Thanks for your interesting.