关于 .net:EF 4.1 Null 插入的 GUID 不为空 | 珊瑚贝

EF 4.1 Null inserted GUID which is not null


我不断收到此错误:

Cannot insert the value NULL into column ‘Id’, table
‘database.dbo.Employees’; column does not allow nulls. INSERT
fails.\
\
The statement has been terminated.

但是对象的这个属性是用 Guid.NewGuid()

设置的

我使用实体框架 4.1 和 .net 框架 4.0。

该列是主键且不为空。我没有使用 Code First 方法。

编辑:如果我手动编辑文件 edmx.

,运行自定义工具似乎不会更新文件


如果您在模型中使用 Fluent API 或数据注释将密钥属性标记为自动生成的身份,则可能会发生这种情况…

1
2
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }

…(其效果是 EF 不会将 Id 值发送到数据库,因为它希望在那里生成密钥)但是在您的数据库表中您没有指定默认值Id 列的值为 newid(),因此数据库不会生成新键。

如果您想手动将 Guid 值从客户端发送到数据库,请从您的模型中删除 DatabaseGeneratedOption.Identity 规范。

  • 我发现了问题,但您建议的答案不是问题。
  • 我会给 1 分,因为你在 EF 中教了我一些新东西。但似乎如果我手动编辑文件,运行自定义工具将无法正确更新实体


导致错误的问题是运行自定义工具没有完全更新实体。 SQL 的插入有效,因此我尝试从 Slauma 建议的更改和实体的其他更改中恢复。


来源:https://www.codenong.com/13100070/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9159.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?