EF5数据库迁移:如何移动数据

时间:2013-11-14 21:24:11

标签: entity-framework database-migration sql-scripts

我首先使用EF5代码,现在我需要更新我的数据库,因此我启用了数据库迁移并添加了迁移,但生成的代码不是我需要的。这是代码:

public override void Up()
    {
        CreateTable(
            "dbo.HistoricalWeightEntities",
            c => new
                {
                    PatientMedicalDataId = c.Guid(nullable: false),
                    Id = c.Guid(nullable: false),
                    Weight = c.Single(nullable: false),
                    Date = c.DateTime(nullable: false),
                })
            .PrimaryKey(t => new { t.PatientMedicalDataId, t.Id })
            .ForeignKey("dbo.PatientMedicalDataEntities", t => t.PatientMedicalDataId, cascadeDelete: true)
            .Index(t => t.PatientMedicalDataId);

        AddColumn("dbo.PatientDataEntities", "PatientDataFilePath", c => c.String());
        //Here I need to move data from the old Weight column to the Weight column on the newly
        //created table and create the id (Guid) and the foreing key before the old 
        //column is dropped
        DropColumn("dbo.PatientMedicalDataEntities", "Weight");
    }

我需要做的是添加一些sql脚本,将数据从dbo.PatientMedicalDataEntities中的'Weight'列移动到新创建的表dbo.HistoricalWeightEntities中的Weight列,并插入Id值(键)是一个Guid和删除列之前的相应外键。 有人可以告诉我这是怎么做的是sql?

提前谢谢

1 个答案:

答案 0 :(得分:6)

它应该是那样的(恩诺你想用Date栏做什么)

Sql("INSERT INTO HistoricalWeightEntities(Id, Weight, PatientMedicalDataId) "+
    "SELECT newid(), Weight, <theForeignKeyColumn> from PatientMedicalDataEntities");