1

I am using .NET 3.1.21 and I'm getting the error:

'CollectionNavigationBuilder<ApplicationUser, ClassroomGroup>' does not contain a definition for 'WithMany' and no accessible extension method 'WithMany' accepting a first argument of type 'CollectionNavigationBuilder<ApplicationUser, ClassroomGroup>' could be found (are you missing a using directive or an assembly reference?)

The syntax seems to be okay from what I've seen so far.

ApplicationDbContext

protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            foreach (var relationship in builder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
                relationship.DeleteBehavior = DeleteBehavior.Restrict;

            builder.Entity<ApplicationUser>().HasMany(x => x.TeacherGroups).WithOne(x => x.Teacher).OnDelete(DeleteBehavior.SetNull);
            builder.Entity<ApplicationUser>().HasMany(x => x.StudentGroups).WithMany(x => x.Students); //this .WithMany gives me the error

builder.Entity<ApplicationUser>().HasMany(x => x.UserRoles).WithOne(x => x.User)
                .HasForeignKey(x => x.UserId).IsRequired().OnDelete(DeleteBehavior.Cascade);
        }

ApplicationUser

public class ApplicationUser : IdentityUser
    {
        public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
        public virtual ICollection<ClassroomGroup> TeacherGroups { get; set; }
        public virtual ICollection<ClassroomGroup> StudentGroups { get; set; }
        public virtual ICollection<Task> CreatedTasks { get; set; }
        public virtual ICollection<SchoolClassStudentTerm> SchoolClassStudentTerms { get; set; }
        public virtual ICollection<GroupAssignment> CreatedGroupAssignments { get; set; }
        public bool Disabled { get; set; }
        public String FullName { get; set; }
        public DateTime? StartTime { get; set; }
    }

ClassroomGroup

public class ClassroomGroup
    {
        public int SubjectId { get; set; }
        public virtual Subject Subject { get; set; }
        public virtual SchoolClass SchoolClass { get; set; }
        public int SchoolClassId { get; set; }
        public String TeacherId { get; set; }
        public virtual ApplicationUser Teacher { get; set; }
        public int Id { get; set; }
        public String Title { get; set; }
        public virtual ICollection<ApplicationUser> Students { get; set; }
        public int TermId { get; set; }
        public virtual Term Term { get; set; }
        public virtual ICollection<GroupAssignment> GroupAssignments { get; set; }
    }

All packages are up to date

Krexci
  • 11
  • 2
  • You can refer to the [link](https://stackoverflow.com/questions/42337911/ef-core-many-to-many-configuration-not-working-with-fluent-api) and check what `Mike Brind` said. – Yiyi You Dec 06 '21 at 07:08
  • I already have it configured this way – Krexci Dec 07 '21 at 09:34

0 Answers0