0

我知道默认情况下 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'.

我怎样才能解决我的问题?

4

1 回答 1

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;
于 2013-03-26T19:51:45.263 回答