2012-07-25 50 views
2

我有以下使用實體框架的C#代碼代碼優先方法。這些表格是在數據庫中創建的;但輸入的數據不正確。實體框架:使用多對多關係時數據不正確

人1俱樂部1的構件和俱樂部3.

人2是俱樂部2的部件和俱樂部3

這意味着俱樂部2僅具有一個成員。

但是使用下面的查詢可以看出數據庫中的數據是不正確的。

需要在C#代碼中進行哪些更改才能使其正確?

enter image description here

static void Main(string[] args) 
    { 

     Database.SetInitializer<NerdDinners>(new MyInitializer()); 
     string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30"; 

     using (var db = new NerdDinners(connectionstring)) 
     { 

      Club club1 = new Club(); 
      Club club2 = new Club(); 
      Club club3 = new Club(); 

      Person p1 = new Person(); 
      Person p2 = new Person(); 


      List<Club> clubsForPerson1 = new List<Club>(); 
      clubsForPerson1.Add(club1); 
      clubsForPerson1.Add(club3); 

      List<Club> clubsForPerson2 = new List<Club>(); 
      clubsForPerson2.Add(club2); 
      clubsForPerson2.Add(club3); 

      List<Person> personInClub1 = new List<Person>(); 
      personInClub1.Add(p1); 

      List<Person> personInClub2 = new List<Person>(); 
      personInClub2.Add(p2); 

      List<Person> personInClub3 = new List<Person>(); 
      personInClub3.Add(p1); 
      personInClub3.Add(p2); 


      club1.Members=personInClub1; 
      club2.Members=personInClub2; 
      club3.Members=personInClub3; 


      p1.Clubs = clubsForPerson1; 
      p2.Clubs = clubsForPerson2; 


      db.Clubs.Add(club1); 
      db.Clubs.Add(club2); 
      db.Clubs.Add(club3); 

      db.Persons.Add(p1); 
      db.Persons.Add(p2); 


      int recordsAffected = db.SaveChanges(); 


     } 

    } 

namespace LijosEF 
{ 

public class Person 
{ 
    public int PersonId { get; set; } 
    public virtual ICollection<Club> Clubs { get; set; } 
} 

public class Club 
{ 
    public int ClubId { get; set; } 
    public virtual ICollection<Person> Members { get; set; } 
} 


public abstract class PaymentComponent 
{ 

    public int PaymentComponentID { get; set; } 
    public int MyValue { get; set; } 
    public abstract int GetEffectiveValue(); 
} 

public partial class GiftCouponPayment : PaymentComponent 
{ 

    public override int GetEffectiveValue() 
    { 
     if (MyValue < 2000) 
     { 
      return 0; 
     } 
     return MyValue; 
    } 

} 

public partial class ClubCardPayment : PaymentComponent 
{ 
    public override int GetEffectiveValue() 
    { 
     return MyValue; 
    } 
} 

public partial class Payment 
{ 
    public int PaymentID { get; set; } 
    public List<PaymentComponent> PaymentComponents { get; set; } 
    public DateTime PayedTime { get; set; } 

} 


public class MyInitializer : CreateDatabaseIfNotExists<NerdDinners> 
{ 
    //Only one identity column can be created per table. 
    protected override void Seed(NerdDinners context) 
    { 


    } 
} 



//System.Data.Entity.DbContext is from EntityFramework.dll 
public class NerdDinners : System.Data.Entity.DbContext 
{ 

    public NerdDinners(string connString): base(connString) 
    { 

    } 

    protected override void OnModelCreating(DbModelBuilder modelbuilder) 
    { 
     //Fluent API - Plural Removal 
     modelbuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
    } 

    public DbSet<Person> Persons { get; set; } 
    public DbSet<Club> Clubs { get; set; } 




} 
} 
+2

你肯定club2具有ID = 2?你的代碼在哪裏? – 2012-07-25 06:29:59

+0

@AmiramKorach我沒有明確地在代碼中設置它。 – Lijo 2012-07-25 06:31:08

+2

所以也許club3.id = 2? – 2012-07-25 06:31:35

回答