1

我首先使用 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 列,并插入作为 Guid 的 Id 值(键)以及删除列之前的相应外键。有人可以告诉我如何做到这一点是 sql 吗?

先感谢您

4

1 回答 1

7

它应该是这样的(不知道你想用 Date 列做什么)

Sql("INSERT INTO HistoricalWeightEntities(Id, Weight, PatientMedicalDataId) "+
    "SELECT newid(), Weight, <theForeignKeyColumn> from PatientMedicalDataEntities"); 
于 2013-11-14T23:11:45.103 回答