Linq to sql 导致外键约束错误

发布于 2024-07-29 16:58:45 字数 1126 浏览 3 评论 0原文

这是相关代码。

       TournamentTeam newTeam = new TournamentTeam();
        TournamentTeams.InsertOnSubmit(newTeam);
        SubmitChanges();

        TournamentParticipant newSignup = new TournamentParticipant
        {
            CheckedIn = false,
            TournamentID = tournamentId,
            UserID = participant.UserID,
            TeamID = newTeam.TeamId
        };

        TournamentParticipants.InsertOnSubmit(newSignup);
        SubmitChanges();

TournamentParticipants.TeamId在TournamentTeam.TeamID上有fk关系,TeamID是一个身份列

我不明白的是,当TournamentTeam获取插入时,它会获取新的身份值。 即使当我调试代码时,新注册也会收到新的团队 ID。 但在生成插入时,它完全避免了该值,并在获取新标识列的 select 语句之前进行插入。

exec sp_executesql N'INSERT INTO [dbo].[TournamentParticipants]([UserID], [TournamentID], [CheckedIn]) VALUES (@p0, @p1, @p2)

SELECT [t0].[TeamID] FROM [dbo].[TournamentParticipants] AS [t0] WHERE ([t0].[UserID] = @p3) AND ([t0].[TournamentID] = @p4)',N'@p0 int,@p1 int,@p2 bit,@p3 int,@p4 int',@p0=29805,@p1=247,@p2=0,@p3=29805,@p4=247

如何使 linq to sql 使用我指定的团队 id 的值或使 select 语句在 insert 语句之前生成?

Here is the revelant code.

       TournamentTeam newTeam = new TournamentTeam();
        TournamentTeams.InsertOnSubmit(newTeam);
        SubmitChanges();

        TournamentParticipant newSignup = new TournamentParticipant
        {
            CheckedIn = false,
            TournamentID = tournamentId,
            UserID = participant.UserID,
            TeamID = newTeam.TeamId
        };

        TournamentParticipants.InsertOnSubmit(newSignup);
        SubmitChanges();

TournamentParticipants.TeamId has a fk relationship on TournamentTeam.TeamID, TeamID is an identity column

What I don't understand is that when TournamentTeam gets inserts, it grabs the new identity value. Even when I debug the code new Signup is recieving the new team id. But when it comes to generating the insert, it avoids this value completely and does the insert before the select statement where it grabs the new identity column.

exec sp_executesql N'INSERT INTO [dbo].[TournamentParticipants]([UserID], [TournamentID], [CheckedIn]) VALUES (@p0, @p1, @p2)

SELECT [t0].[TeamID] FROM [dbo].[TournamentParticipants] AS [t0] WHERE ([t0].[UserID] = @p3) AND ([t0].[TournamentID] = @p4)',N'@p0 int,@p1 int,@p2 bit,@p3 int,@p4 int',@p0=29805,@p1=247,@p2=0,@p3=29805,@p4=247

How can I either make linq to sql use the value for team id that I have specified or make the select statement be generated before the insert statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你再特殊 2024-08-05 16:58:45

不要设置 TeamID,而是将 Team 实体设置为您刚刚创建的实体。 延迟 SubmitChanges 以插入两者,然后在插入完成时修复 id。

TournamentTeam newTeam = new TournamentTeam();
TournamentTeams.InsertOnSubmit(newTeam);

TournamentParticipant newSignup = new TournamentParticipant
{
    CheckedIn = false,
    TournamentID = tournamentId,
    UserID = participant.UserID,
    Team = newTeam
};

TournamentParticipants.InsertOnSubmit(newSignup);
SubmitChanges();

Instead of setting the TeamID, set the Team entity to the one you just created. Delay the SubmitChanges to insert both, then it will fix up the ids when the insert is done.

TournamentTeam newTeam = new TournamentTeam();
TournamentTeams.InsertOnSubmit(newTeam);

TournamentParticipant newSignup = new TournamentParticipant
{
    CheckedIn = false,
    TournamentID = tournamentId,
    UserID = participant.UserID,
    Team = newTeam
};

TournamentParticipants.InsertOnSubmit(newSignup);
SubmitChanges();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文