简体   繁体   English

主键自动递增

[英]Primary key auto increment

Problems: Previously i called the function below. 问题:以前我叫下面的函数。

ArchiveDeletedPatient.ID = 18
_archiveDB2.patient.Add(ArchiveDeletedPatient);
_archiveDB2.SaveChanges();

Afterward I deleted all records from patient database(delete from patient), now in my database it is empty. 之后,我从患者数据库中删除了所有记录(从患者中删除),现在在我的数据库中它是空的。 I called the above function again, however this time it auto increment ID to 19 and inserted into the database. 我再次调用了上面的函数,但是这一次它将自动将ID递增到19并插入数据库。 I spamed the above functions and it kept incrementing. 我向上面的函数添加了垃圾邮件,并且它不断增加。 How to remove such function?? 如何删除这种功能? I don't need the auto increment. 我不需要自动递增。

Some might say it is because of database setting. 有人可能说这是因为数据库设置。 I tried manually insert using Microsoft SQL Server Management Studio 17 "INSERT INTO PATIENT TABLE VALUES(18, ...). It allowed me to insert and when there is such ID in the table. It will not auto increment and will just prompt me a error. How can i remove such auto incrementing feature in c# linq. 我尝试使用Microsoft SQL Server Management Studio 17手动插入“ INSERT INTO PATIENT TABLE VALUES(18,...)。它允许我插入表格,并且当表中有这样的ID时。它不会自动递增,只会提示我我该如何在c#linq中删除这种自动递增功能。

Auto increment feature is not of linq c#. 自动递增功能不是linq c#的。 See the table properties and can you edit the pk to remove "identity" for remove autoincrement o add "identity". 请参阅表属性,您是否可以编辑pk以删除“身份”以删除自动增量或添加“身份”。 If identity is activated in bbdd, the id is calculated in database and linq return this into your entity. 如果在bbdd中激活了身份,则将在数据库中计算ID,然后linq将其返回到您的实体中。

The auto incrementing is not the feature of Linq. 自动递增不是Linq的功能。 It's the feature of Database. 这是数据库的功能。 Here are complete instructions to remove auto increment from MS SQLServer: 以下是从MS SQLServer删除自动增量的完整说明:

  • Open your Database. 打开您的数据库。
  • Right click on your Table and select Design. 右键单击表,然后选择设计。
  • Select your specific column on which you want to remove auto incrementing. 选择要删除自动递增的特定列。
  • In the column properties window click on Identity specification field and make this unchecked. 在列属性窗口中,单击“身份规范”字段,然后将其取消选中。
  • Save table design. 保存表设计。

Here it is... 这里是...

在此处输入图片说明

Note: 注意:

If you're using Entity Framework and specifying your field with [DatabaseGenerated(DatabaseGeneratedOption.Identity)] remove this annotation 如果您使用的是Entity Framework,并使用[DatabaseGenerated(DatabaseGeneratedOption.Identity)]指定字段,请删除此注释

or 要么

replace it with [DatabaseGenerated(DatabaseGeneratedOption.None)] . 将其替换为[DatabaseGenerated(DatabaseGeneratedOption.None)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM