【问题标题】:Error with many-to-many relationship in Entity Framework实体框架中的多对多关系错误
【发布时间】:2016-04-07 23:39:53
【问题描述】:

我是 asp.net 的新手(使用版本 5 MVC),但我正在努力学习它。 我想将 DbContext 用于多对多关系,并尝试查看几个教程。

我弄错了 - “Collection Navigation Builder 不包含方法 WithMany”

我很困惑,非常感谢您的帮助。

我的两个模型类与 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"));

        }
    }
}

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




    }
}

【问题讨论】:

    标签: c# database entity-framework


    【解决方案1】:

    您的模型看起来不错,但您的流畅代码已关闭。你试过了吗:

    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

    【讨论】:

    • 如果我弄错了,我很抱歉,但我已经尝试通过复制粘贴来使用您的代码。我收到一个错误,模型构建器在当前上下文中不存在。如果我使用以前使用过的构建器,问题仍然存在。你能解释一下吗?感谢您的回答。
    • 是的,这就是我插入的代码。但是我仍然遇到了我在 OP 中描述的错误。感谢您的回答。
    • 所以它不喜欢 .WithMany(t => t.Caretakers) ?您使用的是实体框架 6 吗?你有“使用 System.Data.Entity.ModelConfiguration;”在顶部?
    • 不,这些是我的依赖项。 “EntityFramework.Commands”:“7.0.0-rc1-final”,“EntityFramework.MicrosoftSqlServer”:“7.0.0-rc1-final”,谢谢您的回复
    • 是的,使用 EF 7,您需要手动创建联结表,如此处所示。根据您的需要,EF 6.1.3 非常稳定并且可以与 MVC 5 一起使用。stackoverflow.com/questions/29442493/…
    猜你喜欢
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 2011-12-17
    • 1970-01-01
    相关资源
    最近更新 更多