【发布时间】:2017-12-04 10:13:01
【问题描述】:
我通过实体框架代码优先创建了一个表,主键设置为自动递增,但现在我想从列中删除该自动递增。我已经尝试使用流利的 API 来做到这一点:
public class ProductTypeMap: EntityTypeConfiguration<ProductType>
{
public ProductTypeMap()
{
// This is an enum effectively, so we need fixed IDs
Property(x => x.ProductTypeId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
}
还有一个注解:
public class ProductType
{
[Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ProductTypeId { get; set; }
public string ProductTypeName { get; set; }
}
在这两种情况下,它们都会产生相同的迁移代码:
public partial class removeproducttypeidentity : DbMigration
{
public override void Up()
{
DropPrimaryKey("dbo.ProductTypes");
AlterColumn("dbo.ProductTypes", "ProductTypeId", c => c.Int(nullable: false));
AddPrimaryKey("dbo.ProductTypes", "ProductTypeId");
}
public override void Down()
{
DropPrimaryKey("dbo.ProductTypes");
AlterColumn("dbo.ProductTypes", "ProductTypeId", c => c.Int(nullable: false, identity: true));
AddPrimaryKey("dbo.ProductTypes", "ProductTypeId");
}
}
但是,当我在数据库上运行该迁移时,身份规范并未从 SQL Server 2008 数据库表中删除?
我还尝试如下在迁移中显式关闭身份,但也没有这样做:
AlterColumn("dbo.ProductTypes", "ProductTypeId", c => c.Int(nullable: false, identity: false));
还有其他方法告诉 SQL 删除身份吗?
【问题讨论】:
标签: c# entity-framework sql-server-2008 entity-framework-6