繁体   English   中英

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

[英]EF5 Database Migrations: How to move data

我首先使用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吗?

先感谢您

它应该是这样的(不要知道您要使用“日期”列做什么)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM