实体框架迁移,对现有数据进行非规范化并引入外键

时间:2016-10-20 10:32:42

标签: c# entity-framework ef-migrations

我有一个现有数据库(EF代码优先),其中包含表示枚举(c#)的表(table1)中的数据,然后是另一个包含一列的表(table2)枚举值。

我需要做的是非规范化table1,以便它具有table2的外键(枚举值)

就我开始使用的实体而言:

public class EnvironmentTypes 
{
    [Key]
    public int EnvironmentTypeId { get; set; }

    [Required]
    [MaxLength(100)]
    public string Name { get; set; }

    [Required]
    [MaxLength(200)]
    public string Description { get; set; }
}

代表table1

public class EnvironmentDetails
{
    [Required]
    [Index("IX_AppUserMachine", 1, IsUnique = true)]
    [MaxLength(200)]
    public string ApplicationName { get; set; }

    [Index("IX_AppUserMachine", 2, IsUnique = true)]
    [MaxLength(200)]
    public string MachineName { get; set; }

    [Index("IX_AppUserMachine", 3, IsUnique = true)]
    [MaxLength(50)]
    public string UserName { get; set; }

    [Required]
    public EnvironmentType EnvironmentType { get; set; }
}

代表table2

并且在更改之后我需要使table2的实体变为

public class EnvironmentDetails
{
    [Required]
    [Index("IX_AppUserMachine", 1, IsUnique = true)]
    [MaxLength(200)]
    public string ApplicationName { get; set; }

    [Index("IX_AppUserMachine", 2, IsUnique = true)]
    [MaxLength(200)]
    public string MachineName { get; set; }

    [Index("IX_AppUserMachine", 3, IsUnique = true)]
    [MaxLength(50)]
    public string UserName { get; set; }

    [Required]
    [ForeignKey(nameof(EnvironmentTypeId))]
    public virtual EnvironmentTypes EnvironmentType { get; set; }

    public int EnvironmentTypeId { get; set; }
}

创建迁移提供:

public partial class DenormaliseEnvironmentTypeFromEnvironmentDetails : DbMigration
{
    public override void Up()
    {
        AddColumn("dbo.EnvironmentDetails", "EnvironmentTypeId", c => c.Int(nullable: false);
        CreateIndex("dbo.EnvironmentDetails", "EnvironmentTypeId");
        AddForeignKey("dbo.EnvironmentDetails", "EnvironmentTypeId", "dbo.EnvironmentTypes", "EnvironmentTypeId", cascadeDelete: true);
        DropColumn("dbo.EnvironmentDetails", "EnvironmentType");
    }

    public override void Down()
    {
        AddColumn("dbo.EnvironmentDetails", "EnvironmentType", c => c.Int(nullable: false));
        DropForeignKey("dbo.EnvironmentDetails", "EnvironmentTypeId", "dbo.EnvironmentTypes");
        DropIndex("dbo.EnvironmentDetails", new[] { "EnvironmentTypeId" });
        DropColumn("dbo.EnvironmentDetails", "EnvironmentTypeId");
    }
}

适用于空数据库,但如果我在table2中有数据,则运行迁移会引发与外键约束有关的错误。

我的想法是,我应该根据之前的数据填充新的EnvironmentTypeId列,因此我尝试修改迁移,以便向defaultValueSql添加ColumnBuilder参数:

AddColumn("dbo.EnvironmentDetails", "EnvironmentTypeId", c => c.Int(nullable: false, defaultValueSql: "(select [EnvironmentTypeId] from [dbo].[EnvironmentTypes] where [dbo].[EnvironmentTypes].[EnumId] = [EnvironmentType])"));

然而这会产生错误

Error Number:1046,State:1,Class:15
Subqueries are not allowed in this context. Only scalar expressions are allowed.

有没有办法在不丢失数据(或整合)的情况下执行此迁移

2 个答案:

答案 0 :(得分:2)

在@Александр-Пашкин的答案的帮助下,我设法手动修改迁移以维护现有数据。我最终使用的迁移看起来像:

CreateTable("tmp", c => new
{
    DetailsId = c.Int(),
    TypeId = c.Int()
});
Sql("INSERT INTO [dbo].[tmp] select d.Id as DetailsId, t.EnvironmentTypeId as TypeId from [dbo].[EnvironmentDetails] d inner join [dbo].[EnvironmentTypes] t on d.EnvironmentType = t.EnumId");
AddColumn("dbo.EnvironmentDetails", "EnvironmentTypeId", c => c.Int(nullable: false, defaultValue: 1));
CreateIndex("dbo.EnvironmentDetails", "EnvironmentTypeId");
AddForeignKey("dbo.EnvironmentDetails", "EnvironmentTypeId", "dbo.EnvironmentTypes", "EnvironmentTypeId", cascadeDelete: true);
DropColumn("dbo.EnvironmentDetails", "EnvironmentType");
Sql("UPDATE [dbo].[EnvironmentDetails] SET [EnvironmentTypeId] = (SELECT t.[TypeId] from [dbo].[tmp] t where t.DetailsId = Id)");
DropTable("tmp");

因此需要创建一个临时表来根据当前数据保存表之间的链接

根据需要修改数据模型

然后从临时表中填充现有的table2

最后放下临时表。

答案 1 :(得分:1)

您可以使用DbMigration的Sql方法并删除defaultValueSql

Sql("select [EnvironmentTypeId] from [dbo].[EnvironmentTypes] 
     where [dbo].[EnvironmentTypes].[EnumId] = [EnvironmentType]");