【发布时间】:2014-02-16 11:36:12
【问题描述】:
我有一个包含 CodeFirst 数据库(实体框架 6)和两个迁移步骤的项目。 在 Global.asax 的 Application_Start 中使用此代码自动更新数据库:
Database.SetInitializer(
new MigrateDatabaseToLatestVersion<MyDBEntities, MyNamespace.Configuration>());
迁移的第一步是创建表:
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);
第二个迁移步骤是向创建的表添加身份:
AlterColumn("dbo.GalleryAlbum", "Id", c => c.Int(nullable: false, identity: true));
AlterColumn("dbo.GalleryPics", "Id", c => c.Int(nullable: false, identity: true));
当我运行应用程序时,我可以看到第二个迁移代码正在运行,关于两个迁移的信息被添加到 _MigrationHistory 表中,但是两个表中的列都没有改变(没有身份)。这是架构:
[Id] INT NOT NULL,
//other columns
第一次迁移的 Code First 类如下:
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
您能否告诉我,为什么没有将身份添加到这些列中以及如何解决?
谢谢。
更新: 生成对数据库的更新请求,我从 IDbCommandInterceptor 获得:
ALTER TABLE [dbo].[GalleryAlbum] ALTER COLUMN [Id] [int] NOT NULL
ALTER TABLE [dbo].[GalleryPics] ALTER COLUMN [Id] [int] NOT NULL
【问题讨论】:
标签: c# entity-framework entity-framework-6 ef-code-first entity-framework-migrations