C# EF6不更新,仅插入

C# EF6不更新,仅插入,c#,sql-server-2008-r2,entity-framework-6,C#,Sql Server 2008 R2,Entity Framework 6,我在SQLServerDB中有以下结构 公司->分公司->分公司电话->电话号码 所以,公司有很多分支机构,有很多电话号码。分支到电话号码是多对多 以下是实体类: public class Company { public Company() { this.Branches = new HashSet<Branch>(); } [Key] //identity column i

我在SQLServerDB中有以下结构

公司->分公司->分公司电话->电话号码

所以,公司有很多分支机构,有很多电话号码。分支到电话号码是多对多

以下是实体类:

    public class Company
    {
       public Company()
       {
         this.Branches = new HashSet<Branch>();
       }

       [Key] 
       //identity column in sql server
       public int Id{ get; set; }
       public string Name{ get; set; }
       public virtual ICollection<Branch> Branches{ get; set; }
    }

    public partial class Branch
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Branch"/> class.
        /// </summary>
        public Branch()
        {
            this.TelephoneNumbers = new HashSet<TelephoneNumber>();          
        }

        [Column("BranchID")]
        [Key]
        public int Id { get; set; }

        public int CompanyId{ get; set; }

        public virtual Company Company { get; set; }

        public virtual ICollection<TelephoneNumber> TelephoneNumbers { get; set; } 
   }

public partial class TelephoneNumber
{
    public TelephoneNumber()
    {
      this.Branches = new HashSet<Branch>();
    }   

    [Key]
    [Column("TelephoneID")]
    public int Id { get; set; }

    public virtual ICollection<Branch> Branches { get; set; }    
 }
此时,通过查看SQAl Server Profiler和运行SQL,所有数据都是 正确插入

using(var c2 = new MyDbContext())
 {
    var company2 = c2 .Companies.First(x=>x.Name == "C1" );
    var b1 = company2 .Branches.First();

    //data matches what is inserted above

    b1.TelephoneNumbers.Clear();
    b1.TelephoneNumbers.Add(new TelephoneNumber() );

    company2.Name = "Updated";

    c2.SaveChanges();
 }
在sql server上生成上面的save changes followign语句

1. Update Company set name  = "Updated" -- Correct
2. Delete from BranchTelephone the two entries that were added during first insert
3. Insert New Company
4. Insert new Branch
5. Insert new Telephone Number
6. Insert new Branch Telephone
7. Insert new Telephone Number
8. Insert new Branch Telephone
9. Insert new Telephone Number
10. Insert new Branch Telephone
因此,基本上它会按预期进行更新和删除

然后从第一步开始重新创建所有内容,这样我总共有3个电话号码,而不是1个

我不知道这里发生了什么。任何帮助都将不胜感激

多谢各位

我到处玩,结果

如果我使用以下命令,一切都很好: b1.电话号码。添加新电话号码

我的意思是,它可以更新数据,删除所有相关的电话号码。但是,如果我在同一上下文中执行上述操作,那么它会忘记所有其他内容,只插入所有新的符号,即新的公司、分支机构和3个电话号码,包括两个已删除的号码


这是不能接受的。我在这里做错了什么

问题原来是一个实体属于一个单独的dbcontext实例。因此,context1用于获取entityA实例,该实例被添加到context2.entityBCollection中,导致EF插入所有内容,而不仅仅是entityA实例


我认为这是EF中的某种缺陷,因为如果entityA没有连接到contextB,那么它应该抛出一个异常,或者只在数据库中创建entityA。但是,contextB将附加到contextB的整个对象层次结构的插入发送到数据库。

您需要将子对象标记为已修改。这是一种痛苦。您也可以使用GraphDiff。这不是问题所在。其中一个实体来自单独的db上下文实例,因此创建了所有这些。
1. Update Company set name  = "Updated" -- Correct
2. Delete from BranchTelephone the two entries that were added during first insert
3. Insert New Company
4. Insert new Branch
5. Insert new Telephone Number
6. Insert new Branch Telephone
7. Insert new Telephone Number
8. Insert new Branch Telephone
9. Insert new Telephone Number
10. Insert new Branch Telephone