简体   繁体   中英

SQL in migrationBuilder Incorrect syntax near the keyword

Usually, I can find answers here, but the error I'm getting is not specific enough for me to figure it out myself, beyond the fact that there must be a typo of some kind. I honestly don't know what's the typo though. I hope somebody can help.

I'm using .NET Core & EntityFramework (which is still new to me). I've created a couple of Migrations: 'initial' & 'populate'. 'populate' is an empty migration which I intended to use for seeding.

protected override void Up(MigrationBuilder migrationBuilder)
{
    foreach (var job in GetAllJobs().OrderBy(x => x.Id))
            migrationBuilder.Sql("INSERT INTO Jobs (Id, Code, Name, Role, Type, Group) " +
                                 "VALUES ('" + job.Id + "', '" + job.Code + "', '" + job.Name + "', '" + job.Role + "', '" + job.Type + "', '" + job.Group + "');");
}

For some reason, when running dotnet ef database update from command-line, it throws the aforementioned error: "Incorrect syntax near the keyword 'Group'" , but I really don't see why.

http://i.imgur.com/it5rP5P.png

I've tried omitting the semicolon at the end of the SQL statement, but that results in the same error. I really don't know what could possibly be wrong with the SQL statement :(

Any help would be greatly appreciated.

SOLUTION:

protected override void Up(MigrationBuilder migrationBuilder)
{
    foreach (var job in GetAllJobs().OrderBy(x => x.Id))
            migrationBuilder.Sql("INSERT INTO Jobs (Id, Code, Name, Role, Type, [Group]) " +
                                 "VALUES ('" + job.Id + "', '" + job.Code + "', '" + job.Name + "', '" + job.Role + "', '" + job.Type + "', '" + job.Group + "');");
}

将括号放在标识符周围,GROUP是保留字

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM