实体框架代码首先 - 创建表并将现有表中的数据插入到新创建的表中,

时间:2016-07-18 09:42:40

标签: c# entity-framework ef-code-first ef-migrations

我首先使用EF代码,我想创建一个新表并将现有表中的值插入其中。

public override void Up()
        {
            CreateTable(
                "dbo.UserCulture",
                c => new
                {
                    UserCultureId = c.Int(false, true),
                    Id = c.Int(false),
                    IsSelected = c.Boolean(false),
                    Culture = c.String(false, 10),
                    UserLevel = c.Int(false),
                    CurrentScore = c.Int(false),
                    TimelinePercentage = c.String(false, 3),
                    NumberOfGamesPlayed = c.Int(false)

                })
                .PrimaryKey(k => k.UserCultureId)
                .ForeignKey("dbo.AskUser", k => k.Id)
                .Index(t => t.Id);

            Sql("INSERT INTO UserCulture(UserCultureId, Id, IsSelected, Culture, UserLevel, CurrentScore, TimelinePercentage, NumberOfGamesPlayed)" +
                "SELECT newid(), UserId, 1, CreatedCulture, UserLevel, CurrentScore, TimelinePercentage, NumberOfGamesPlayed FROM AskUser");

        }

当我运行此迁移时,它创建了表但它是空的。有谁知道我应该做什么?

1 个答案:

答案 0 :(得分:0)

newid()是针对uniqueidentifier的吗? 您可以尝试在insert语句中删除该列吗?

Sql("INSERT INTO UserCulture(Id, IsSelected, Culture, UserLevel, CurrentScore, TimelinePercentage, NumberOfGamesPlayed)" +
                "SELECT UserId, 1, CreatedCulture, UserLevel, CurrentScore, TimelinePercentage, NumberOfGamesPlayed FROM AskUser");