[英]Entity framework code first - create table and insert data from existing table into newly created one,
I'm using EF code first and I want to create a new table and insert values from an existing table into it. 我首先使用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");
}
When I ran this migration it created the table but it was empty. 当我运行此迁移时,它创建了表,但是它是空的。 Does anyone know what I should do?
有谁知道我该怎么办?
newid() is for uniqueidentifier? newid()是否用于uniqueidentifier? Can you try removing that column in the insert statement?
您可以尝试在插入语句中删除该列吗?
Sql("INSERT INTO UserCulture(Id, IsSelected, Culture, UserLevel, CurrentScore, TimelinePercentage, NumberOfGamesPlayed)" +
"SELECT UserId, 1, CreatedCulture, UserLevel, CurrentScore, TimelinePercentage, NumberOfGamesPlayed FROM AskUser");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.