Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么EF中的add方法仍然添加重复项?_C#_Asp.net_Duplicates_Entity Framework 6 - Fatal编程技术网

C# 为什么EF中的add方法仍然添加重复项?

C# 为什么EF中的add方法仍然添加重复项?,c#,asp.net,duplicates,entity-framework-6,C#,Asp.net,Duplicates,Entity Framework 6,我正在尝试使用现有客户向数据库中添加一台新机器,但每次都会将该客户作为副本添加到数据库中 代码: public bool Create(ConfiguratedMachine entity) { int addedRecords; using (var ctx = _dbContext) { var cus = ctx.Customers.Find(3); ctx.Customers.Attach(cus);

我正在尝试使用现有客户向数据库中添加一台新机器,但每次都会将该客户作为副本添加到数据库中

代码:

public bool Create(ConfiguratedMachine entity)
 {
     int addedRecords;
     using (var ctx = _dbContext)
     {
         var cus = ctx.Customers.Find(3);
         ctx.Customers.Attach(cus);
         var configmach = ctx.ConfiguratedMachines.Add(entity.ToContextModel());

         addedRecords = ctx.SaveChanges();
        }
     return addedRecords > 0;
  }
public bool Create(ConfiguratedMachine entity)
        {
            int addedRecords;
            using (var ctx = _dbContext)
            {
                var cus = ctx.Customers.Find(3);
                ctx.Customers.Attach(cus);
                var configMachine = entity.ToContextModel();
                configMachine.Customer = cus;
                var configmach = ctx.ConfiguratedMachines.Add(configMachine);
                addedRecords = ctx.SaveChanges();
            }
            return addedRecords > 0;
        }
数据库结果:

customer表有一个标识(1,1),这可能会造成干扰吗?我的意思是,即使列上有一个自动增量,也应该可以添加,对吗

调试结果:

public bool Create(ConfiguratedMachine entity)
 {
     int addedRecords;
     using (var ctx = _dbContext)
     {
         var cus = ctx.Customers.Find(3);
         ctx.Customers.Attach(cus);
         var configmach = ctx.ConfiguratedMachines.Add(entity.ToContextModel());

         addedRecords = ctx.SaveChanges();
        }
     return addedRecords > 0;
  }
public bool Create(ConfiguratedMachine entity)
        {
            int addedRecords;
            using (var ctx = _dbContext)
            {
                var cus = ctx.Customers.Find(3);
                ctx.Customers.Attach(cus);
                var configMachine = entity.ToContextModel();
                configMachine.Customer = cus;
                var configmach = ctx.ConfiguratedMachines.Add(configMachine);
                addedRecords = ctx.SaveChanges();
            }
            return addedRecords > 0;
        }
这张图片显示数据库中确实有一个id为3的客户

这张图片显示,现有的客户是附加的,但有一个错误,我不清楚

这表明机器已与客户一起添加

这表明添加了2条记录,因此客户和机器。。。


我做错了什么

我发现了我的错误,我试图从上下文模型转换为域模型。这一点是为了从一个对象转换到另一个对象而创建的新对象

丑陋的快速解决方案:

public bool Create(ConfiguratedMachine entity)
 {
     int addedRecords;
     using (var ctx = _dbContext)
     {
         var cus = ctx.Customers.Find(3);
         ctx.Customers.Attach(cus);
         var configmach = ctx.ConfiguratedMachines.Add(entity.ToContextModel());

         addedRecords = ctx.SaveChanges();
        }
     return addedRecords > 0;
  }
public bool Create(ConfiguratedMachine entity)
        {
            int addedRecords;
            using (var ctx = _dbContext)
            {
                var cus = ctx.Customers.Find(3);
                ctx.Customers.Attach(cus);
                var configMachine = entity.ToContextModel();
                configMachine.Customer = cus;
                var configmach = ctx.ConfiguratedMachines.Add(configMachine);
                addedRecords = ctx.SaveChanges();
            }
            return addedRecords > 0;
        }

我觉得你说得不太对。attach方法的要点是让上下文跟踪对以前未跟踪的实体的更改。没有理由对刚使用上下文从数据库中获取的实体调用attach。您应该使用find方法获取现有客户,或者使用正确的ID附加新创建的客户实体。如果操作的唯一目的是分配客户和机器之间的关系,则后一种解决方案将避免往返数据库