【问题标题】:EF5 Database Migrations: How to move dataEF5 数据库迁移:如何移动数据
【发布时间】:2013-11-28 02:46:43
【问题描述】:

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

提前谢谢你

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-12-02
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多