简体   繁体   中英

How to do Many To Many relationship in Ef Core using Fluent API?

I'm trying to create a many to many relation in the Fluent API with Ef Core 6 but i am having trouble understanding how to do so.

I've looked around here in stackoverflow but couldn't understand this relation and how to reproduce it in my code.

I have a table in my SQL database called People:

People.cs:

 public class People : PeopleBase
    {
        public People()
        {
            RegistrationList = new HashSet<Registration>();
        }

        public virtual ICollection<Registration> RegistrationList { get; set; }

        public virtual User User { get; set; }

        public virtual ActivityGroup ActivityGroup { get; set; }
    }

PeopleBase.cs:

 public abstract class PeopleBase: ModelBase
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid PeopleId { get; set; }

        public Guid? UserId { get; set; }

        public Guid? ActivityGroupId { get; set; }

        public string Code { get; set; }

        public string Name { get; set; }

        public string Surname { get; set; }

        public PeopleActiveType Active { get; set; }
    }

And then i have another table called ActivityGroup:

ActivityGroup.cs:

public class ActivityGroup : ActivityGroupBase
    {
        public ActivityGroup()
        {
            PeopleList = new HashSet<People>();
            ActivityList = new HashSet<Activity>();
        }

        public ICollection<People> PeopleList { get; set; }

        public ICollection<Activity> ActivityList { get; set; }
    }

ActivityGroupBase.cs:

public abstract class ActivityGroupBase : ModelBase
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid ActivityGroupId { get; set; }

        [Required]
        [StringLength(20)]
        public string Name { get; set; }

        public StatusRecord Status { get; set; }
    }

How would i do the mapping in the modelBuilder given that:

  • ActivityGroupId is the foreing key in the People database, pointing to the other table
  • One PeopleId can have multiple (many) ActivityGroupId
  • One ActivityGroupId can be assigned to multiple people.

What i've done so far:

modelBuilder.Entity<People>()
   .HasOne(x => x.ActivityGroup)
   .WithMany(x => x.PeopleList)
   .HasForeignKey(x => x.ActivityGroupId);

Wouldn't i have to do this instead?

modelBuilder.Entity<People>()
   .Hasmany(x => x.ActivityGroupList) //this is a ICollection<ActivityGroup> inside People class
   .WithMany(x => x.PeopleList)
   .HasForeignKey(x => x.ActivityGroupId); // this is not recognized by Ef Core

Can anyone help me please?

There are two main approaches for many-to-many relationships - with implicit junction table:

public class People : PeopleBase
{
   // ...
   public virtual List<Activity> Activities { get; set; }
}

public class Activity // : ...
{
   // ...
   public virtual List<People> PeopleList { get; set; }
}

modelBuilder.Entity<People>()
   .HasMany(x => x.Activities)
   .WithMany(x => x.PeopleList); 

Or with explicit one :

public class People
{
  // ...
  public ICollection<PeopleActivity> PeopleActivities { get; set; }
}

public class Activity
{
  // ...
  public virtual ICollection<PeopleActivity> PeopleActivities { get; set; }
}

public class PeopleActivity
{
   public Guid ActivityId { get; set; }
   public Guid PeopleId { get; set; }
   public Activity Activity { get; set; }
   public People People { get; set; }
}
modelBuilder.Entity<PeopleActivity>()  
   .HasOne(pt => pt.People)           
   .WithMany(t => t.PeopleActivities) 
   .HasForeignKey(pt => pt.PeopleId); 

Also maybe it worth changing entity name from People to Person (you can change table name with .ToTable("People") call)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM