简体   繁体   English

实体的主键字段设置为0而不是null

[英]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应该能够弄清楚PersonIDPerson的键,但以防万一,请尝试在其中粘贴[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 . 由于PersonIDint因此永远不能为null Thus Entity Framework assigns 0 as default value. 因此, Entity Framework0分配为默认值。 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.

相关问题 无法跟踪实体,因为主键为空 - Unable to track an entity because primary key is null 如何通过特定字段(不是主键)获取实体? - How to get entity by specific field (not primary key)? 无法首先使用实体​​框架代码将其他指定的列设置为主键,而不是将ID列设置为表 - unable to set other specified column as primary key instead of Id column to table using entity framework code first 设置实体框架与非主键字段的一对一映射 - Set Entity Framework one-to-one mapping with non primary key field 使用实体框架插入实体后主键保持为空 - Primary key stays null after inserting Entity with Entity Framework 实体框架代码优先 - 非主键字段的外键 - Entity Framework Code First - Foreign Key to non primary key field 如果DataKeyNames设置为未定义为主键的字段,则… - If DataKeyNames is set to a field not defined as a primary key, then… 编辑已设置为主键的字段 - Editing a field that has been set as the primary key ASP.NET MVC 6 - 为实体框架7中的主键分配NOT NULL - ASP.NET MVC 6 - Assigning NOT NULL to the Primary Key in Entity Framework 7 无法跟踪类型的实体,因为主键属性“id”为空 - Unable to track an entity of type because primary key property 'id' is null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM