【问题标题】:EF6 not updating, only insertsEF6 不更新,只插入
【发布时间】:2015-05-12 01:37:36
【问题描述】:

我在 SQL Server DB 中有以下结构

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

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

这里是实体类:

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

我尝试执行以下操作来测试设置:

var t1 = new TelephoneNumber();
var t2 = new TelephoneNumber();
using(var context = new MyDbContext() )
{
   var company = new Company
   {
     Name = "C1",
     Branches = 
     {
       new Branch
       {
         TelephoneNumbers = {t1, t2}
       }
     }          
   };
   context.SaveChanges();
 }

此时查看 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 上生成跟随上述保存更改的 Atfer 语句

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.TelephoneNumbers.Add(new TelephoneNumber());

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

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

【问题讨论】:

  • 您需要将您的子对象标记为已修改。这是一种痛苦。您也可以使用 GraphDiff。 stackoverflow.com/questions/24789903/…
  • 这就是问题所在。其中一个实体来自一个单独的数据库上下文实例,因此创建了所有这些。

标签: c# sql-server-2008-r2 entity-framework-6


【解决方案1】:

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

我认为它是 EF 中的某种错误,因为如果 entityA 未附加到 contextB ,它应该抛出异常或仅在数据库中创建 entityA。但是,contextB 将附加到 contextB 的整个对象层次结构的插入发送到数据库。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    • 2021-10-20
    相关资源
    最近更新 更多