【问题标题】:Entity Framework: Incorrect data while using Many-to-many relationship实体框架:使用多对多关系时数据不正确
【发布时间】:2012-07-25 06:15:44
【问题描述】:

我有以下使用 Entity Framework Code First 方法的 C# 代码。表是在数据库中创建的;但输入的数据不正确。

个人 1 是俱乐部 1 和俱乐部 3 的成员。

个人 2 是俱乐部 2 和俱乐部 3 的成员

这意味着 Club 2 只有一名成员。

但是使用下面的查询可以看出数据库中到达的数据是不正确的。

需要对 C# 代码进行哪些更改才能使其正确?

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

    
    
}
}

【问题讨论】:

  • 您确定 club2 的 id=2 吗?你的代码在哪里?
  • @AmiramKorach 我没有在代码中明确设置它。
  • @AmiramKorach 是的。这就是发生的事情。这里建议的方法是什么?
  • 如果是这种情况,这里没有问题。我认为您以错误的方式使用 id 。您可能希望添加另一列用作商家 ID,并且可以根据需要控制其值。

标签: c# .net entity-framework ef-code-first


【解决方案1】:

其实没有问题。由于 id 创建的顺序,我认为这是一个问题。我在Entity Framework: Duplicate Records in Many-to-Many relationship 中发布了一些其他问题。

它使用

((IObjectContextAdapter)db).ObjectContext.Attach((IEntityWithKey)entity); 

【讨论】:

    猜你喜欢
    • 2017-01-28
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多