Greenmachine
Greenmachine

Reputation: 292

Error with many-to-many relationship in Entity Framework

I'm new to asp.net(using version 5 MVC),but I'm trying to learn it. I want to use DbContext for a many to many relationship and have tried looking at several tutorials.

I'm get a mistake - "Collection Navigation Builder does not contain method WithMany"

I'm puzzled and would strongly appreciate the help.

My two model classes are posted beneath with the db context.

namespace WebApplication2.Models
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<ApplicationUser> ApplicationUsers { get; set; }
        public DbSet<Caretaker> CareTakers { get; set; }
        public DbSet<Child> Children { get; set; }
        public DbSet<Fee> Fees { get; set; }
        public DbSet<Feetype> Feetypes { get; set; }
        public DbSet<Message> Messages { get; set; }
        public DbSet<Photo> Photos { get; set; }

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

            //
            builder.Entity<Caretaker>()
                  .HasMany<Child>(c => c.Children)
                  .WithMany
    //      .WithMany(s => s.ApplicationUsers)
       //   .Map(t => t.MapLeftKey("CourseId")
        //  .MapRightKey("StudentId")
        //  .ToTable("CourseStudent"));

        }
    }
}

namespace WebApplication2.Models {

public class Caretaker
{

    public Caretaker(string Name, string LastName, string Gender, DateTime Bday, string Email,string Phone) {
        this.Name = Name;
        this.LastName = LastName;
        this.Gender = (Gender)Enum.Parse(typeof(Gender), Gender);
        this.Bday = Bday;
        this.Email = Email;
        this.Phone = Phone;
        Children = new List<Child>();

    }

    [ScaffoldColumn(false)]
    public int CareTakerID { get; set; }

    public Gender Gender { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
    public DateTime Bday { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }



    public virtual ICollection<Child> Children { get; set; }
}

}

using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;

namespace WebApplication2.Models
{

  public enum Gender { Male, Female }

    public class Child
    {

     public Child(string Name,string LastName, string Gender,DateTime Bday)
      {
         this.Name = Name;
         this.LastName = LastName;
         this.Gender = (Gender)Enum.Parse(typeof(Gender), Gender);
         this.Bday = Bday;
         Photos = new List<Photo>();
         Caretakers = new List<Caretaker>();
         ApplicationUsers = new List<ApplicationUser>();
         Fees = new List<Fee>();
        }

        [ScaffoldColumn(false)]
        public int ChildID { get; set; }

        public String Name { get; set; }
        public String LastName { get; set; }

        public Gender Gender { get; set; }

        public DateTime Bday { get; set; }


        public  ICollection<Photo> Photos { get; set; }
        public virtual ICollection<Caretaker> Caretakers { get; set; }
        public virtual ICollection<ApplicationUser> ApplicationUsers { get; set; }

        public ICollection<Fee> Fees { get; set; }




    }
}

Upvotes: 2

Views: 2001

Answers (1)

Steve Greene
Steve Greene

Reputation: 12304

Your models look OK, but your fluent code is off. Have you tried:

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

    builder.Entity<Caretaker>() 
           .HasMany(t => t.Children) 
           .WithMany(t => t.Caretakers) 
           .Map(m => { 
                       m.ToTable("CaretakerChild"); 
                       m.MapLeftKey("CaretakerID"); 
                       m.MapRightKey("ChildID"); 
                     });
}

https://msdn.microsoft.com/en-us/data/jj591620.aspx?f=255&MSPPError=-2147217396#ManyToMany

Upvotes: 1

Related Questions