2013-11-14 47 views
1

我首先使用EF5代码,现在我需要更新数据库,因此我启用了数据库迁移并添加了迁移,但生成的代码不是我所需要的。这是代码: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"); 
    } 

我需要做的是添加从“权重”列在dbo.PatientMedicalDataEntities在新创建的表dbo.HistoricalWeightEntities将数据移动到权重列的一些SQL脚本,并在列被删除之前,还要插入作为Guid的Id值(键)和相应的外键。 有人可以告诉我如何做到这一点是SQL?

预先感谢您

回答

5

它应该是类似的东西(donnow你想与日期列做什么)

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

@althaus非常感谢您的回答,它的正常工作。 – jcgalveza