2016-04-07 23 views
1

Asp.net'te (sürüm 5 MVC kullanarak) yeni yaşıyorum ancak öğrenmeye çalışıyorum. DbContext'i bir çok ilişki için kullanmak istiyorum ve birkaç öğreticiye bakmayı denedim.Varlık Çerçevesi'nde çoktan çoğa ilişkiyle ilgili hata

Ben şaşkın ve yardıma şiddetle takdir ediyorum "Koleksiyon Navigasyon Oluşturucu yöntemi WithMany içermiyor" - Ben

bir hata olsun.

İki model sınıfım db içeriğinin altına yazılmıştır.

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")); 

     } 
    } 
} 

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




    } 
} 

cevap

1

Modelleriniz Tamam bak, ama akıcı kod kapalıdır. denedin:

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

+0

ben karıştırıyorsun ediyorsam üzgünüm ama bunu copypasting tarafından size kodu kullanarak denedim. Model üreticisinin geçerli bağlamda bulunmadığı konusunda bir hata aldım. Daha önce kullandığım oluşturucuyu kullanırsam sorun devam eder. Lütfen açıklar mısınız? Cevabı takdir ediyorum. – Greenmachine

+1

Tamam, senin yapıcısını aradın. Cevapları düzenler. –

+0

Evet, eklediğim kod bu. Yine de OP'de açıkladığım hatayı hala yaşıyorum. Cevabınızı takdir ediyorum. – Greenmachine

İlgili konular