nGAGE
nGAGE

Reputation: 177

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

Upvotes: 0

Views: 2735

Answers (1)

ErikEJ
ErikEJ

Reputation: 41819

Put bracket around identifiers, GROUP is a reserved word

Upvotes: 2

Related Questions