EF add data in OnModelCreating in a many many relationship

Viewed 181

I created

public class Video
{
    public int Id { get; set; }
            [MaxLength(50)]
    public string Nom { get; set;}
    public  ICollection<Langue> Langue { get; set; }
}

public class Langue
    {
        public int LangueId { get; set; }
        [Required]
        public string Designation { get; set; }
        public  ICollection<Video> Video { get; set; }
    }

Migration is ok and 3 tables are created, Video, Langue and VideoLangue with 2 fields VideoId and LangueId It’s a many to many relationship.

I want to entrer data in OnModelCreating

public class VideoDbContext : DbContext
{
    public VideoDbContext(DbContextOptions<VideoDbContext> option) : base(option)
    {

    }
    public DbSet<Video> Video { get; set; }
    public DbSet<Langue> Langue { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

 
        var langFR = new Langue() { LangueId = 1, Designation = "Français" };
        var langIT = new Langue() { LangueId = 2, Designation = "Italien" };
        var langCH = new Langue() { LangueId = 3, Designation = "Chinois" };
        var langAN = new Langue() { LangueId = 4, Designation = "Anglais" };

        modelBuilder.Entity<Video>().HasData(
        
 new Video() { Id = 1, Nom = "007", Langue = new List<Langue> { langFR,langCH,langIT } }
         );

When i do the migration i have this error

The seed entity for entity type 'Video' cannot be added because it has the navigation 'Langue' set. To seed relationships, add the entity seed to 'VideoLangue (Dictionary<string, object>)' and specify the foreign key values {'VideoId'}. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the involved property values.

According to this error i use an other way i create a VideoLangue Class

public class VideoLangue
{
    public int VideoId { get; set; }
    public int LangueId { get; set; }
}

I modify the VideoContext

public class VideoDbContext : DbContext
    {
        public VideoDbContext(DbContextOptions<VideoDbContext> option) : base(option)
        {

        }
        public DbSet<Video> Video { get; set; }
        public DbSet<Langue> Langue { get; set; }
        public DbSet<VideoLangue> VideoLangue { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

     
            modelBuilder.Entity<Langue>().HasData(
            //remplissage langue
            new Langue() { LangueId = 1, Designation = "Français" },
            new Langue() { LangueId = 2, Designation = "Italien" },
            new Langue() { LangueId = 3, Designation = "Chinois" },
            new Langue() { LangueId = 4, Designation = "Anglais" }
            );

        modelBuilder.Entity<Video>().HasData(
            
     new Video() { Id = 1, Nom = "007}
             );
modelBuilder.Entity<VideoLangue>()
           .HasKey(o => new { o.VideoId, o.LangueId });

            modelBuilder.Entity<VideoLangue>().HasData(
            ////remplissage VideoLangue
            new VideoLangue() { VideoId = 1, LangueId = 1 },
            new VideoLangue() { VideoId = 1, LangueId = 3 },
            new VideoLangue() { VideoId = 1, LangueId = 4 }
            
            );

When i do the migration i have Cannot use table 'VideoLangue' for entity type 'VideoLangue (Dictionary<string, object>)' since it is being used for entity type 'VideoLangue' and potentially other entity types, but there is no linking relationship. Add a foreign key to 'VideoLangue (Dictionary<string, object>)' on the primary key properties and pointing to the primary key on another entity typed mapped to 'VideoLangue'.

How can we put data on langue via OnModelreating Do you have an idea ? Thank you

0 Answers
Related