2013-11-14 52 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