C# 为什么 EF 试图在 id 列中插入 NULL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8855100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is EF trying to insert NULL in id-column?
提问by Kovpaev Alexey
I am writing my project using Entity Framework 4.0 (Model first). At the beginning of the project, I faced with this problem: I am trying to insert the filled object in the database, but I get an exeption:
我正在使用 Entity Framework 4.0(模型优先)编写我的项目。在项目开始时,我遇到了这个问题:我试图在数据库中插入填充的对象,但我得到了一个例外:
Cannot insert the value NULL into column 'CategoryId', table 'ForumDB.dbo.Categories'; column does not allow nulls. INSERT fails. The statement has been terminated.
无法将值 NULL 插入列“CategoryId”、表“ForumDB.dbo.Categories”;列不允许空值。插入失败。该语句已终止。
Code:
代码:
Category usingCategory = new Category("Using Forums", "usingforums", 0);
using (Context)
{
Context.Categories.AddObject(usingCategory);
Context.SaveChanges();
}
I checked this object, and I am sure that it is filled.
我检查了这个对象,我确定它被填满了。
Just in case:
以防万一:
public Category(string name, string urlName, int index)
{
CategoryId = Guid.NewGuid();
Name = name;
UrlName = urlName;
CategoryIndex = index;
}
What is going on?
到底是怎么回事?
采纳答案by akiller
Have a look at this: https://stackoverflow.com/a/5338384/171703- entity framework might be assuming that your CategoryId field is an identity and therefore passing null to the database expecting it to fill it for you.
看看这个:https: //stackoverflow.com/a/5338384/171703- 实体框架可能假设您的 CategoryId 字段是一个身份,因此将 null 传递给数据库,希望它为您填充它。
回答by D.B. Fred
I ran into this today and had to regenerate my EF classes from the database.
我今天遇到了这个问题,不得不从数据库中重新生成我的 EF 类。
After doing that, I found that EF added:
这样做之后,我发现EF添加了:
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
to this"Id" field that used to be an Identity column in the SQL but was changed to be app-assigned.
到这个“Id”字段,该字段曾经是 SQL 中的标识列,但已更改为应用程序分配。
I think if you don't have that attribute EF won't actually send the ID to the database ('convention over configuration')
我认为如果您没有该属性,EF 实际上不会将 ID 发送到数据库(“约定优于配置”)
回答by Adam Cox
I had created the table with int Id as PK, but had forgotten to set "Identity Specification" = True
我用 int Id 作为 PK 创建了表,但忘记设置“身份规范”= True
回答by Vikas Sharma
Met the same problem today.
今天遇到同样的问题。
Here is how I figured out the issue.
这是我如何解决这个问题的。
I had added the identity seed to the column initially but after that I removed it. So I did not realize that when modifying the column definition in the Code first, the Id
我最初将身份种子添加到列中,但之后我将其删除。所以我没有意识到,在Code中先修改列定义的时候,Id
Property(x => x.Id).HasColumnName(@"Id").HasColumnType("int").IsRequired();
属性(x => x.Id).HasColumnName(@"Id").HasColumnType("int").IsRequired();
So Entity framework figured out that it was an Identity column and I got the exception above.
所以实体框架发现它是一个身份列,我得到了上面的例外。
To fix this, added the "HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None)" so the final piece was something like:
为了解决这个问题,添加了“ HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None)”所以最后一块是这样的:
Property(x => x.Id).HasColumnName(@"Id").HasColumnType("int").IsRequired().HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None);
属性(x => x.Id).HasColumnName(@"Id").HasColumnType("int").IsRequired().HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None);
回答by Damir Varevac
Try to add this into your model class .cs file:
尝试将其添加到您的模型类 .cs 文件中:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CategoryId { get; set; }
Or change your column CategoryId to identity:
或者将您的列 CategoryId 更改为身份:
CategoryId int IDENTITY(1,1)
回答by marifrahman
For me I had a different schema other than default one. The name of the Id key was some how missing the that schema part - fixed that in the database and it all went well. How it was ;
对我来说,我有一个不同于默认架构的架构。Id 键的名称在某种程度上缺少该架构部分 - 在数据库中修复了它并且一切顺利。它怎么样 ;
PK_TableName
How I changed it to
我如何将其更改为
PK_mySchema.TableName
回答by tayfun K?l??
?f Model first try block
?f 模型第一次尝试块
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public Int64 PolicyID { get; set; }
回答by tayfun K?l??
0
0
?f you dont use code first or db first oracle dont create be sequnce You try it trigger with sequence
?如果您不先使用代码或先使用数据库 oracle 不要创建序列,您可以尝试使用序列触发
CREATE OR REPLACE TRIGGER
"a"."IHALEYOL"
before insert on "a"."IHALEYOL"
for each row
begin
select "a"."SQ_DIREK".nextval into :new."ID" from dual;
end;
seq:
序列:
create sequence SQ_YETKI
minvalue 1
maxvalue 9999999999999999999999999999
start with 221
increment by 1
cache 20;

