在迁移期间将标识设置为先前创建的列 [英] Set identity to the previous created column during migration

查看:89
本文介绍了在迁移期间将标识设置为先前创建的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有CodeFirst数据库(实体框架6)和两个迁移步骤的项目. 通过使用Global.asax的Application_Start中的以下代码自动更新数据库:

I have a project with the CodeFirst database (Entity Framework 6) and two migration steps. Database is updated automatically by using this code in Application_Start in Global.asax:

Database.SetInitializer(
          new MigrateDatabaseToLatestVersion<MyDBEntities, MyNamespace.Configuration>());

第一个迁移步骤是创建表:

First migration step is creating the tables:

CreateTable(
     "dbo.GalleryAlbum",
      c => new
      {
           Id = c.Int(nullable: false),
           //other columns.....
       })
       .PrimaryKey(t => t.Id);

CreateTable(
       "dbo.GalleryPics",
       c => new
       {
           Id = c.Int(nullable: false),
           //other columns.....
       })
       .PrimaryKey(t => t.Id)
       .ForeignKey("dbo.GalleryAlbum", t => t.AlbumId)
       .Index(t => t.AlbumId);

第二个迁移步骤是将标识添加到已创建的表中

Second migration step is adding identities to the created tables:

AlterColumn("dbo.GalleryAlbum", "Id", c => c.Int(nullable: false, identity: true));
AlterColumn("dbo.GalleryPics", "Id", c => c.Int(nullable: false, identity: true));

运行该应用程序时,可以看到第二个迁移代码正在运行,有关两次迁移的信息已添加到_MigrationHistory表中,但两个表中的列均未更改(没有Identity).这是模式:

When I run the application, I can see that second migration code is running, information about two migrations is added to the _MigrationHistory table, but columns in both tables are not changed (without Identity). Here is the schema:

[Id]        INT             NOT NULL,
//other columns

首次迁移的代码优先类如下:

Code First classes for the first migration are the following:

 public partial class GalleryAlbum
 {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }
 }
 //GalleryPics is the same

这是第二个迁移步骤:

public partial class GalleryAlbum
 {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
 }
 //GalleryPics is the same

能否请您告诉我,为什么没有将身份添加到这些列中以及如何解决?

Could you please tell me, why Identity is not added to these columns and how I can fix it?

谢谢.

更新: 生成的对数据库的更新请求,这是我从IDbCommandInterceptor获得的:

Update: Generated update requests to the database, which I got from the IDbCommandInterceptor:

ALTER TABLE [dbo].[GalleryAlbum] ALTER COLUMN [Id] [int] NOT NULL
ALTER TABLE [dbo].[GalleryPics] ALTER COLUMN [Id] [int] NOT NULL

推荐答案

您不能在SQL Server中将列更改为Identity,请参见

You can't ALTER a column to Identity in SQL Server, see Adding an identity to an existing column

相反,请尝试一步添加标识和列:

Instead try add identity and column in one step:

CreateTable(
         "dbo.GalleryAlbum",
          c => new
          {
              Id = c.Int(nullable: false, identity:true),
              //other columns.....
          }).PrimaryKey(t => t.Id);

这篇关于在迁移期间将标识设置为先前创建的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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