EF4通过脚本插入数据

时间:2013-03-26 19:21:42

标签: sql-server sql-server-2008 entity-framework-4 identity-insert

我知道默认情况下,EF Code First会设置StoreGeneratedPattern = Identit。没关系。但我想通过sql脚本插入一些数据。我知道只有一张表格可以SET IDENTITY_INSERT Rules ON;

但是当我尝试运行时

USE [GameDatabase]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

--RULES OF GAME
SET IDENTITY_INSERT RulesOfGames ON;
INSERT INTO RulesOfGames(RulesOfGameId, MaxScore, IsPossibleEqualsScore) VALUES(1, 20, 1);
SET IDENTITY_INSERT RulesOfGames OFF;

--KindSport
SET IDENTITY_INSERT RulesOfGames ON;
INSERT INTO KindSports(KindSportId, Name, RulesOfGame_RulesOfGameId) VALUES(1, 'Футбол', 1);
SET IDENTITY_INSERT KindSports OFF;

我收到错误。我想设置特定的id来设置特定的逻辑约束。

Msg 8107, Level 16, State 1, Line 3
IDENTITY_INSERT is already ON for table 'GameDatabase.dbo.KindSports'. Cannot perform SET operation for table 'RulesOfGames'.

我如何解决我的问题?

1 个答案:

答案 0 :(得分:0)

SET IDENTITY_INSERT RulesOfGames ON;
INSERT INTO RulesOfGames(RulesOfGameId, MaxScore, IsPossibleEqualsScore) VALUES(1, 20, 1);
SET IDENTITY_INSERT RulesOfGames OFF;

--KindSport
SET IDENTITY_INSERT KindSports ON;
INSERT INTO KindSports(KindSportId, Name, RulesOfGame_RulesOfGameId) VALUES(1, 'Футбол', 1);
SET IDENTITY_INSERT KindSports OFF;