LINQ to SQL:SubmitChanges()不起作用? [英] LINQ to SQL: SubmitChanges() does not work?

查看:177
本文介绍了LINQ to SQL:SubmitChanges()不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

var bt = new BachtuocvnDataContext();
        var matchedTeams = (from lt in bt.Bet_Leagues_Teams
                         where lt.LeagueID == leagueID
                         select (lt)).Single();
        matchedTeams.TeamID = teamID;
        bt.SubmitChanges(ConflictMode.ContinueOnConflict);

它不会更新表.传统查询效果很好,我在这里找到了类似的问题:

It does not update the table. Traditional query works well, I found a similar question here:

LINQ不在.SubmitChanges()上更新

但是我检查并发现Bet_Leagues_Teams确实具有主键.

but I checked and found that Bet_Leagues_Teams does have a primary key.

Bet_Leagues_Teams班:

Bet_Leagues_Teams class:

int ID (primary key)
int LeagueID;
int TeamID;

啊,我的可怕过错.我忘记了Bet_Leagues_Teams可能不包含所需的记录.我必须检查记录是否存在,然后更新它,否则它不存在,必须将其添加到表中.真可惜原谅我浪费你的时间.谢谢.

Ahhhh, MY TERRIBLE MISTAKE. I forgot that Bet_Leagues_Teams may not contains the record needed. I must check if the record existed, and then update it, or it does not exist and I must add it to the table. Shame on me. Forgive me for wasting your time. Thank you.

推荐答案

using(BachtuocvnDataContext bt = new BachtuocvnDataContext() ) 
{ 
    Bet_Leagues_Teams matchedTeam = 
        bt.Bet_Leagues_Teams.Where(lt => lt.LeagueID == leagueID)
        .SingleOrDefault(); 

    if(matchedTeam != null)
    {
        matchedTeam.TeamID = teamID; 
        bt.SubmitChanges(ConflictMode.ContinueOnClonflict); 
    }
} 

这篇关于LINQ to SQL:SubmitChanges()不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆