简体   繁体   中英

How insert values with static Id in Entity Framework for SQL Server CE?

I am trying fill new table in manually migration.

I successfully created the table, but I have trouble with inserting.

var sql = @"CREATE TABLE[DefaultSettings] ([Id] int IDENTITY (1, 1)  NOT NULL
                                           , [Tag] nvarchar(63)  NOT NULL
                                           , [ValueInt] int NULL
                                           , [ValueDate] datetime NULL
                                           , [ValueChar] nvarchar(255)  NULL);";
dbContext.Database.ExecuteSqlCommand(sql);

sql = @"ALTER TABLE[DefaultSettings] ADD CONSTRAINT[PK_DefaultSettings] PRIMARY KEY ([Id])";
dbContext.Database.ExecuteSqlCommand(sql);

sql = @"SET IDENTITY_INSERT [DefaultSettings] ON;
INSERT INTO [DefaultSettings] ([Id], [Tag], [ValueInt], [ValueDate],[ValueChar]) VALUES (1, N'DefaultArea', 58, NULL, NULL);
INSERT INTO [DefaultSettings] ([Id], [Tag], [ValueInt], [ValueDate],[ValueChar]) VALUES (2, N'DefaultFarm', 52, NULL, NULL);
GO";

dbContext.Database.ExecuteSqlCommand(sql);
return;

For second ExecuteSqlCommand I get errors

Error statement insert

I am try separate all commands in sql variable by GO - I know that SQL Server CE cannot into multiple SqlCommand .

But I got error like "error statement GO".

Any ideas?

SQL Server Compact仅允许每个命令使用一个语句,因此您必须对ExecuteSqlCommand使用多个调用,每个INSERT语句使用一个。

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