EF Code First 6.1将ID从Int更改为Long [英] EF Code First 6.1 Change ID from Int to Long

查看:56
本文介绍了EF Code First 6.1将ID从Int更改为Long的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用ASP.Net MVC 4,C#和Entity Framework Code First的应用程序.有一个模型(表)正在变得越来越大,我担心Identity列将在明年左右用完值(Int32.MaxValue).

I'm working on an application that is using ASP.Net MVC 4, C# and Entity Framework Code First. There is one model (Table) that is growing rather large and I'm worried that the Identity column will run out of values (Int32.MaxValue) in the next year or so.

我正在寻找一种方法来将此标识从int更改为long.在过去,当我尝试这样做时,EF不会让我这样做,除非它删除了表并重新创建了它.这不是一个选择,因为该站点已经处于活动状态,并且此表中有实时数据.

I'm looking for a way to change this identity from int to a long. In the past when I tried this, EF wouldn't let me do this unless if it dropped the table and recreated it. This is not an option since the site is already live and there is live data in this table.

有没有办法做到这一点?到目前为止,我的网络搜索和Stackoverflow搜索都没有给我任何有关如何执行此操作的提示.

Is there a way to do this? So far my web searches and Stackoverflow searches have not given me any hints on how to do this.

推荐答案

需要代码优先迁移:

1)更新您的模型

2)创建您的迁移

3)在生成的迁移中编写自定义SQL,以删除PK和引用它的每个FK.这很麻烦,因为您必须弄清楚名称.将其放在您的AlterColumn上方.

4)编写自定义SQL,以重新添加您的PK和FK,并将其放在AlterColumn下.

5)现在,在您的Down脚本中进行相反的操作.

1) Update your model

2) Create your migration

3) Write custom SQL in your generated migration to drop your PK and every FK that references it. This can get quite messy as you have to figure out the names. Put this above your AlterColumn.

4) Write custom SQL to re-add your PK and FKs and put it under your AlterColumn.

5) Now do the reverse in your Down script.

仅供参考,Azure SQL无法实现.在这种情况下,您必须创建一个新表.

FYI, this isn't possible with Azure SQL. In this case you must create a new table.

这篇关于EF Code First 6.1将ID从Int更改为Long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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