20. Many-to-Many Relationships in Entity Framework Core

Поділитися
Вставка
  • Опубліковано 10 гру 2024

КОМЕНТАРІ • 58

  • @odamuser
    @odamuser 3 роки тому +3

    Man, you saved my life! Clean, easy, perfect! You're awesome, man!! Thank u!

    • @DotNetHow
      @DotNetHow  3 роки тому +1

      You are welcome. Thanks for watching! 🙏

  • @ExpatTraderFX
    @ExpatTraderFX 2 роки тому

    Bravo "shume bukur" explanation

  • @erikipsen1682
    @erikipsen1682 Рік тому

    Nice, simple and strait to the case! Thanks for uploading this video 🙂

  • @renatocarvalho6059
    @renatocarvalho6059 2 роки тому

    Amazingly useful, clean and clear! Thank you very much!

  • @RohitSharma-vq2gn
    @RohitSharma-vq2gn 3 роки тому +1

    This is extremely lucid explanation of both Fluent Api usage and EF Core. Great work, Ervis :)

    • @DotNetHow
      @DotNetHow  3 роки тому

      Thank you so much for your feedback, Rohit! ☺

  • @Engineer2261
    @Engineer2261 7 місяців тому

    Thanks Ervis for the very clear explanation.
    How do you add a primary Author constraint to a book? Let's say one book has only one primary author, all the others are considered secondary. Do we need different models for that or can we simply apply it to the joint table?

    • @DotNetHow
      @DotNetHow  6 місяців тому

      In the joining table you can add a column, isPrimaryAuthor: boolean

  • @yoannhenry6361
    @yoannhenry6361 Рік тому

    Thanks a lot : It helps me in minutes !! Great Job!!

  • @motoviagensmg4400
    @motoviagensmg4400 2 роки тому

    Excellent explanation, very concise.

  • @JustasNomeika
    @JustasNomeika Рік тому

    Thanks for the video.
    Should I create instances of all - Book, Author and Author_Book objects? and then add them to the database?

  • @TheSamuelssmelo
    @TheSamuelssmelo 3 роки тому +2

    You are an amazing teacher. Great content!!

    • @DotNetHow
      @DotNetHow  3 роки тому +1

      Thank you soooo much Samuel

  • @LafsGaming
    @LafsGaming 3 роки тому

    You made me understand this concept... Thanks for that ✌️

  • @sherlockholmes1121
    @sherlockholmes1121 5 місяців тому

    Very Very useful thanks

    • @DotNetHow
      @DotNetHow  5 місяців тому

      Thanks for watching

  • @arohawrami8132
    @arohawrami8132 2 роки тому

    Thanks a lot Ervis. It is a very useful tutorial.

    • @DotNetHow
      @DotNetHow  2 роки тому

      Thanks for watching. I am glad you found them useful.

  • @pritomsarkar2963
    @pritomsarkar2963 3 роки тому +1

    Awesome Sir. But Please try to Create a dynamically multitenant simple web application.

    • @DotNetHow
      @DotNetHow  3 роки тому

      I am building this app about book management. So, you can probably learn the concepts and build that app as your personal project. That would be great

  • @eliumoraes
    @eliumoraes 3 роки тому

    Hello Ervis! Thanks for sharing! I would like to know how can I add more columns in the Books_Authors table like the date to know when the association between this author and book were created.

    • @DotNetHow
      @DotNetHow  3 роки тому

      Add a mew property -> Add-Migration -> Update-Database

  • @pourianayeb1063
    @pourianayeb1063 3 роки тому +1

    I have 2 questions:
    01. How to create composition key for book_authors table, could it be happening by Key attribute or fluent api or maybe both?
    02. I'm totally sure I never use fluent api for many-to-many and it works like a charm, why do we need this additional config?

    • @DotNetHow
      @DotNetHow  3 роки тому +3

      Hello Pouria!
      Regarding your questions:
      1. Yes, you can use a composition key for the books_authors table. In our case it would be:
      modelBuilder.Entity()
      .HasKey(t => new { t.AuthorId, t.BookId });
      2. You do not need additional FluentAPI code if you want to keep the default columns that the entity framework will generate, but if you want to customize them, then you need to use fluent API. I prefer to use fluent API to set the join table names with underscore "_ ".
      In our case, EF Core would generate a table named AuthorBook with two columns, AuthorsId, BooksId

    • @DotNetHow
      @DotNetHow  3 роки тому +1

      I used the default conventions in One-to-Many, but wanted to show that you can customize relations (table names, columns) using Fluent API

    • @pourianayeb1063
      @pourianayeb1063 3 роки тому +1

      @@DotNetHow just got it, thanks 😊

    • @DotNetHow
      @DotNetHow  3 роки тому +1

      @@pourianayeb1063 you are welcome

  • @imermamani2205
    @imermamani2205 2 роки тому

    this is very very easy to learn

  • @fieryscorpion
    @fieryscorpion 3 роки тому

    This is amazing. Thank you!
    If you add authentication and some clean architecture (eg: Domain Driven Design) to your Udemy course, I'll buy and study it ASAP!

  • @EnGamology
    @EnGamology 3 роки тому +1

    This one is the best

  • @Spirittism
    @Spirittism 3 роки тому

    wished you did some interaction with the db using linq

  • @SharpCoder
    @SharpCoder 3 роки тому

    How it use on practice. Related entities does not updates

  • @robxlift
    @robxlift 3 роки тому

    Introducing FOREIGN KEY constraint 'FK_dbo.VendorDetails_dbo.States_StateID' on table 'VendorDetails' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors.

    • @DotNetHow
      @DotNetHow  3 роки тому

      Can you provide additional info, like what are your models, what did you write inside OnModelCreating?

    • @robxlift
      @robxlift 3 роки тому

      @@DotNetHow Introducing FOREIGN KEY constraint 'FK_StudentCourses_Students_StudentId' on table 'StudentCourses' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
      Could not create constraint or index. See previous errors.
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {

      modelBuilder.Entity()
      .HasKey(t => new { t.StudentId, t.CourseId});
      }

      public DbSet Courses { get; set; }
      public DbSet Departments { get; set; }
      public DbSet Students { get; set; }
      public DbSet StudentCourses { get; set; }

    • @robxlift
      @robxlift 3 роки тому

      Models
      in course
      public Department Department { get; set; }
      public List StudentCourses { get; set; }
      in department
      public List Students { get; set; }
      public List Courses { get; set; }
      in student
      public Department Department { get; set; }
      public List StudentCourses { get; set; }
      in studentCourse
      public Student Student { get; set; }
      public Course Course { get; set; }

    • @DotNetHow
      @DotNetHow  3 роки тому

      @@robxlift I do not see the State anywhere in your models (FK_dbo.VendorDetails_dbo.States_StateID). You need to share the full models, otherwise, it is impossible to offer any help.

    • @robxlift
      @robxlift 3 роки тому

      @@DotNetHow i delete old project and make new one , but same error but with new models

  • @LeartBajrami-co1gn
    @LeartBajrami-co1gn 6 місяців тому +1

    valid

  • @Elyeslahmar
    @Elyeslahmar Рік тому

    Je tenais à vous remercier pour la vidéo excellente. J'ai cependant remarqué un point à corriger concernant la table Book_Author. Il n'est pas nécessaire d'ajouter une clé primaire distincte. En fait, la clé primaire peut être composée des clés étrangères AuthorId et BookId.
    Voici la syntaxe correcte à ajouter dans la méthode OnModelCreating de votre classe de contexte DbContext
    /*****modelBuilder.Entity().HasKey(r => new{ r.PermissionId,r.RoleId });***/

  • @asfandalikhan6269
    @asfandalikhan6269 3 роки тому

    Thanks bro

  • @sobrelaroca2701
    @sobrelaroca2701 2 роки тому

    Where is the create? update?

  • @semajxocliw
    @semajxocliw 2 роки тому

    ef core should be able to handle many-to-many automatically as of 5.0

    • @DotNetHow
      @DotNetHow  2 роки тому +1

      That is right. I just wanted to show that it is customisable

    • @qwertz3813
      @qwertz3813 2 роки тому

      But you can customize the key of a pivot if you do it this way.
      Example:
      modelBuilder.Entity
      .HasKey(ma => new { ma.MovieId, ma.ActorId, ma.RoleName });
      where RoleName is something new to both of the tables