2

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; }




    }
}
CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
Greenmachine
  • 292
  • 1
  • 15

1 Answers1

1

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

Steve Greene
  • 12,029
  • 1
  • 33
  • 54
  • I'm sorry if I am mistaking, but I've tried using you code by copypasting it. I've received an error that modelbuilder does not exist in the current context. If I use the builder I have used previously the problem remains. Would you please explain? I appreciate yor answer. – Greenmachine Apr 07 '16 at 15:01
  • Yes, that's the code I've inserted. Yet i still got the error I've described in the OP. I appreciate your answer. – Greenmachine Apr 07 '16 at 15:46
  • So it doesn't like the .WithMany(t => t.Caretakers) ? Are you using Entity Framework 6? Do you have "using System.Data.Entity.ModelConfiguration;" at the top? – Steve Greene Apr 07 '16 at 16:36
  • no,these are the dependecies I have. "EntityFramework.Commands": "7.0.0-rc1-final", "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final", Thank you for your reply – Greenmachine Apr 07 '16 at 17:39
  • Oh, it seems that version 7 is quite raw so far, so that was the problem. Thank you! – Greenmachine Apr 07 '16 at 17:57
  • 1
    Yeah, with EF 7 you will need to manually create the junction table as shown here. Depending on what you need, EF 6.1.3 is pretty stable and works with MVC 5. http://stackoverflow.com/questions/29442493/how-to-create-a-many-to-many-relationship-with-latest-nightly-builds-of-ef7 – Steve Greene Apr 07 '16 at 18:03