简体   繁体   English

为什么我不能在单个服务器请求中插入带有外键的记录?

[英]Why can't I insert record with foreign key in a single server request?

I'm tryring to do a simple insert with foreign key, but it seems that I need to use db.SaveChanges() for every record insert.我正在尝试使用外键进行简单的插入,但似乎我需要为每个记录插入使用db.SaveChanges() How can I manage to use only one db.SaveChanges() at the end of this program?我怎样才能在这个程序结束时只使用一个db.SaveChanges()

public static void Test()
{
    using (var entities = new DBEntities())
    {
        var sale =
            new SalesFeed
            {
                SaleName = "Stuff...",
            };
        entities.AddToSalesFeedSet(sale);

        var phone =
            new CustomerPhone
            {
                CreationDate = DateTime.UtcNow,
                sales_feeds = sale
            };
        entities.AddToCustomerPhoneSet(phone);

        entities.SaveChanges();
    }
}

After running the above code I get this exception:运行上面的代码后,我得到这个异常:

System.Data.UpdateException: An error occurred while updating the entries. System.Data.UpdateException:更新条目时出错。 See the InnerException for details.有关详细信息,请参阅 InnerException。 The specified value is not an instance of a valid constant type Parameter name: value.指定的值不是有效常量类型的实例参数名称:值。

EDIT: Changed example code and added returned exception.编辑:更改了示例代码并添加了返回的异常。

Apperantly using UNSIGNED BIGINT causes this problem.显然使用UNSIGNED BIGINT会导致此问题。 When I switched to SIGNED BIGINT everything worked as it supposed to.当我切换到SIGNED BIGINT一切正常。

I tried to do this "the right way":我试图以“正确的方式”做到这一点:

替代文字

And then I wrote this little test app to scan a directory, store the directory and all its files in two tables:然后我编写了这个小测试应用程序来扫描目录,将目录及其所有文件存储在两个表中:

static void Main(string[] args)
{
   string directoryName = args[0];

   if(!Directory.Exists(directoryName))
   {
      Console.WriteLine("ERROR: Directory '{0}' does not exist!", directoryName);
      return;
   }

   using (testEntities entities = new testEntities())
   {
      StoredDir dir = new StoredDir{ DirName = directoryName };
      entities.AddToStoredDirSet(dir);

      foreach (string filename in Directory.GetFiles(directoryName))
      {
         StoredFile stFile = new StoredFile { FileName = Path.GetFileName(filename), Directory = dir };
         entities.AddToStoredFileSet(stFile);
      }

      try
      {
         entities.SaveChanges();
      }
      catch(Exception exc)
      {
         string message = exc.GetType().FullName + ": " + exc.Message;
      }
   }
}

As you can see, I only have a single call to .SaveChanges() at the very end - this works like a charm, everything's as expected.如您所见,我在最后只对.SaveChanges()进行了一次调用 - 这就像一个魅力,一切都按预期进行。

Something about your approach must be screwing up the EF system.....你的方法一定是搞砸了 EF 系统.....

it might be related with the implementation of AddToSalesFeedSet etc.. there is chance that you are doing commit inside ?它可能与 AddToSalesFeedSet 等的实现有关。你有可能在里面做提交吗?

any way, my point is that i encountered very close problem, was tring to add relation to new entity with existed entity that been queried earlier - that has unsigned key and got the same exception;无论如何,我的观点是我遇到了非常接近的问题,试图添加与先前查询的现有实体的新实体的关系 - 具有未签名的密钥并得到相同的异常;

the solution was to call Db.collection.Attach(previouslyQueriedEntityInstance);解决方案是调用 Db.collection.Attach(previouslyQueriedEntityInstance);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM