Question

I'm using EF5 code first and now I need to update my database so I enabled database migrations and added a migration but the generated code was not what I needed. This is the code:

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");
    }

What I need to do is to add some sql script that move data from the 'Weight' column in the dbo.PatientMedicalDataEntities to the Weight column in the newly created table dbo.HistoricalWeightEntities and also insert the Id value (key) which is a Guid and the corresponding foreign key before the column is dropped. Can somebody show me how to do this is sql?

Thank you in advance

Was it helpful?

Solution

It should be something like that (donnow what you wanna do with the Date column)

Sql("INSERT INTO HistoricalWeightEntities(Id, Weight, PatientMedicalDataId) "+
    "SELECT newid(), Weight, <theForeignKeyColumn> from PatientMedicalDataEntities"); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top