从主键中删除自动增量 [英] remove auto increment from primary key

查看:81
本文介绍了从主键中删除自动增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个代码优先的MVC应用程序中,我有一个表格,其关键字段为Long,并且脚手架使其自动成为自动增量字段。

In an MVC app with code first I have a table with the key field as Long and the scaffolding made it automatic an auto increment field.

这不是我想要的,我想生成自己的密钥。所以我用以下内容注释了关键字段:

that is not what I wanted, I want to generate my own key. So I annotated the key field with:

 [Key]
 [DatabaseGenerated(DatabaseGeneratedOption.None)]

我运行了迁移(Add-Migration和Update-Database)但主键没有任何反应。生成迁移SQL中的sneeking我也找不到任何相关的东西。

I ran the migration (Add-Migration and Update-Database) But nothing happens with the Primary key. sneeking in the Generated Migration SQL I do also not find anything that is relevant.

我该怎么做?

Rob

推荐答案

你好抢战争 ning,

>>我该怎么做?

>>How can I do this??

基于你的说明,我创建了一个简单的演示,并在我的身边重现您的问题。由于迁移无法处理此操作,因此在SQL Server中,您不能仅使用ALTER语句进行更改。

Based on your description, I create a simple demo and reproduce your issue on my side. Because migrations can’t handle this operation yet, in SQL Server you can’t just make the change with an ALTER statement.

以下文档通过自定义迁移操作提供解决方案供您参考。

The following document provide a solution via custom migrations operation for your reference.

https://romiller.com/2013/04/30/ef6-switching-identity-onoff -with-a-custom-migration-operation /

祝你好运,

张龙武


这篇关于从主键中删除自动增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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