简体   繁体   English

实体框架中多对多关系的错误

[英]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.我是 asp.net 的新手(使用版本 5 MVC),但我正在尝试学习它。 I want to use DbContext for a many to many relationship and have tried looking at several tutorials.我想将 DbContext 用于多对多关系,并尝试查看几个教程。

I'm get a mistake - "Collection Navigation Builder does not contain method WithMany"我有一个错误 - “集合导航生成器不包含方法 WithMany”

I'm puzzled and would strongly appreciate the help.我很困惑,非常感谢您的帮助。

My two model classes are posted beneath with the db context.我的两个模型类与 db 上下文一起发布在下面。

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 {命名空间 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; }




    }
}

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 https://msdn.microsoft.com/en-us/data/jj591620.aspx?f=255&MSPPError=-2147217396#ManyToMany

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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