无法将值NULL插入列“ Id”(数据库优先) [英] Cannot insert the value NULL into column 'Id' (Database first)

查看:478
本文介绍了无法将值NULL插入列“ Id”(数据库优先)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库优先解决方案。

我正在使用两个名为 User 和个人资料。他们都使用 uniqueidentifier (SQL Server)作为主键。我让 User 自动接收其自己的Guid ID的方式是:

I'm using two tables called User and Profile. They are both using uniqueidentifier (SQL Server) as their primary key. The way I let User to automatically receive it's own guid id was from:


  1. 打开我的edmx文件。

  1. Open my edmx file.

右键单击用户的Guid列->属性->将StoreGeneratedPattern更改为Identity。

Right click the Guid column for User -> Properties -> change the StoreGeneratedPattern to Identity.

像护身符一样工作。现在,我尝试使用 Review 做同样的事情,但是每次尝试创建新行时都会遇到以下错误:

Worked like a charm. Now I tried to do same with Review, but I'm getting the following error every time I'm trying to create a new row:


无法将值NULL插入表
'xxxx.dbo.Profile'的列'Id'中;列不允许为空。插入失败。
语句已终止。

Cannot insert the value NULL into column 'Id', table 'xxxx.dbo.Profile'; column does not allow nulls. INSERT fails. The statement has been terminated.

如果我不使用 Identity 用于 StoreGeneratedPattern 用于个人资料,它将不会生成唯一的ID,而只会生成一堆零,从而导致如果我尝试再次创建新行,则会出现重复的错误。

If I don't use Identity for StoreGeneratedPattern for Profile, it will not generate a unique ID, instead just bunch of zeros which lead to duplicated error if I try to create a new row again.

为什么用户工作正常,但个人资料没有?

Why is User working fine but not Profile?

我遵循了本指南指南

推荐答案

正如@Gert Arnold在评论中提到的那样,我忘记添加 newsequentialid()配置文件表的$ c>。完成此操作后,我终于可以使用 DatabaseGeneratedOption.Identity 生成唯一的Guid。

As @Gert Arnold mentioned in comments, I forgot to add newsequentialid() for Profiletable in SQL Server. Once I did that, I were finally able to generate unique Guids with DatabaseGeneratedOption.Identity.

您可以阅读更多内容关于此方法的信息此处。这还包括代码优先技术。

You can read more about this method here. This also include techniques for Code-First.

这篇关于无法将值NULL插入列“ Id”(数据库优先)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆