[英]Entity's primary key field is set to 0 instead of null
I have a Person entity type like so: 我有一个Person实体类型,如下所示:
public partial class Person
{
public int PersonID { get; set; }
public byte Gender { get; set; }
public string IDNumber { get; set; }
}
When I create a new Person for the purpose of saving him to the db, the primary key, PersonID, is set to 0, instead of null. 当我创建一个新的Person以便将其保存到数据库时,主键PersonID设置为0,而不是null。
the code: 编码:
var theNewGuy = new MyEntities.Person();
right after this line, theNewGuy.PersonID = 0. The question is, how would I tell it to insert a new Person and assign it the next available primary key? 在此行之后,theNewGuy.PersonID =0。问题是,我如何告诉它插入一个新的Person并为其分配下一个可用的主键? Obviously, I don't want it to be 0, yet there is already a Person in the db with PersonID = 0;
显然,我不希望它为0,但是数据库中已经有一个PersonID = 0的Person;
The save code: 保存代码:
Velo.People.Add(theNewGuy);
Velo.SaveChanges();//throws error
Exception thrown: Violation of PRIMARY KEY constraint 'PK_PERSON'. 引发异常: 违反PRIMARY KEY约束“ PK_PERSON”。 Cannot insert duplicate key in object 'dbo.Person'.
无法在对象“ dbo.Person”中插入重复密钥。 The duplicate key value is (0).
重复键值为(0)。 The statement has been terminated.
该语句已终止。
Primary keys cannot be Null. 主键不能为Null。 A PK must be unique, and Null isn't.
PK必须是唯一的,而Null不是唯一的。 Also,
int
is not a nullable type, so it's default value is 0. 另外,
int
不是可为null的类型,因此它的默认值为0。
EntityFramework should be able to figure out that PersonID
is Person
's key, but just in case try sticking the [Key]
attribute on there. EntityFramework应该能够弄清楚
PersonID
是Person
的键,但以防万一,请尝试在其中粘贴[Key]
属性。 Additionally, you could force it to be an auto increment
column by adding the [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
attribute to your PersonID
property. 另外,您可以通过将
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
属性添加到PersonID
属性来强制将其设为auto increment
列。
Since PersonID
is int
it never can be null
. 由于
PersonID
为int
因此永远不能为null
。 Thus Entity Framework
assigns 0
as default value. 因此,
Entity Framework
将0
分配为默认值。 But no worries - if you will add such newly created Entity to database, everything will be fine - it will be saved, given new PersonID
based on what's already in database, and entity in memory will be refreshed to reflect the new id given by database. 但是不用担心-如果您将这样新创建的实体添加到数据库中,一切都会很好-将被保存,
PersonID
根据数据库中已有的内容给定新的PersonID
,并刷新内存中的实体以反映数据库给出的新ID 。
If you define public int? 如果定义public int? PersonID { get;
PersonID {get; set;
组; } (using ? in the type of object) yo can use null as value.
}(在对象类型中使用?)可以将null用作值。
However, how it has been commented, a primary key it would not be null, but perhaps in you particular case in an itermediate state is can be useful. 但是,如何注释它,主键将不会为null,但是在您处于中间状态的特定情况下可能有用。
If you are creating your entity model from a database, as a primary key it would not be null, in the design of the database you can't set this field as nullable, so you must add the "?" 如果要从数据库创建实体模型,则作为主键,它不会为null,因此在数据库的设计中不能将此字段设置为可为空,因此必须添加“?” modified manually.
手动修改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.