无法将 NULL 插入实体框架中的非标识列。有趣的是......它不为空?
我有一个 SalesOrder 表,其中包含 ID 和 OrderID 列。 ID 是一个自动生成的 int。 OrderID 是一个不可为 null 的字符串,最大长度为 20,我们用它来存储客户的订单号以供参考。
添加新的 SalesOrder 并调用 SaveChanges 后,出现以下错误:
无法将 NULL 值插入列“OrderID”、表“SalesOrder”;列不允许为空。插入失败。 该语句已终止。
问题是,我正在保存的对象实际上确实有一个 OrderID!这几乎就像它在保存所有值之前尝试先保存实体一样。这就是 EF 处理事情的方式吗?
我的设置是 EF4.1,使用 EDMX 模型优先方法。 StoreGeneratePattern 设置为“无”。默认值当前设置为(无),但我尝试了各种值。实体密钥为 False,因为它不是密钥的一部分。我还尝试删除 SalesOrder 实体并从数据库重新生成它。
I have a SalesOrder table with columns for ID and OrderID. ID is an auto-generated int. OrderID is a non-nullable string with a max length of 20, and we use it to store the customer's order number for reference.
After adding my new SalesOrder and calling SaveChanges, I get the following error:
Cannot insert the value NULL into column 'OrderID', table 'SalesOrder'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Problem is, the object that I'm saving actually does have an OrderID! It's almost like it's trying to save the entity first before it saves all the values. Is this how EF handles things?
My setup is EF4.1, using an EDMX model-first approach. StoreGeneratedPattern is set to None. Default Value is currently set to (None) but I've tried various values. Entity Key is False, since it's not part of the key. I've also tried deleting the SalesOrder entity and regenerating it from the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还想看看你的代码...在循环中填充对象然后使用 savechanges 保存它们时,我遇到了类似的问题。我以为所有的田地都有人居住,但事实并非如此。
我必须先查看您在保存更改之前执行的代码,然后才能提供任何真正有用的信息。
如果您的问题像我的一样,并且您在使用迭代器填充对象后调用 savechanges,那么您可以通过将 savechanges 移动到迭代器中以便在每次迭代时调用它来找到错误数据......但这都是假设的猜测没有看到你的代码...
I would also like to see your code...I had similar problems when filling objects in a loop then saving them with savechanges. I thought all the fields were populated, but they were not.
I'd have to see your code that executes before the save changes before I can offer anything really helpful.
If your problem is like mine and you are calling savechanges after using an iterator to populate your objects, then you can find the bad data by moving savechanges into the iterator so that it is called with each iteration...but this is all hypothetical guesswork without seeing your code...