简体   繁体   English

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

[英]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.

相关问题 从表Code First Entity Framework 6中删除标识插入 - Remove Identity Insert from table Code First Entity Framework 6 为什么实体框架代码优先(使用现有数据库)继续尝试从EdmMetadata表获取数据? - Why does Entity Framework Code-First (with an existing DB) keep trying get data from an EdmMetadata table? 实体框架6从现有表创建模型 - Entity Framework 6 Create model from existing table 使用Entity Framework Code First开发,我能否在现有数据库中创建一个或多个表? - Using Entity Framework Code First Development, can I get it to create a table or tables in an existing database? 使用现有数据库和实体框架的代码优先:当IDENTITY_INSERT设置为OFF时,无法为表中的标识列插入显式值 - Code First With Existing Database, Entity Framework: Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF 如何在Entity Framework 6 Code First中的同一张表之间创建一对一关系? - How to create one-to-one relationship between same table in Entity Framework 6 Code First? 实体框架代码优先 - 具有连接/链接表的一对多 - Entity Framework Code First - One-to-Many with a join/link table 使用实体框架Code-First创建表,运行时 - Create Table, Run Time using entity framework Code-First 实体框架6首先将多个表与一个外键关系代码 - Entity Framework 6 multiple table to one foreign key relationship code first Entity Framework Core - 代码优先 - 两个外键 - 一张表 - Entity Framework Core - Code First - Two Foreign Keys - One Table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM