我使用的是ASP.NET MVC 4 (.NET框架4.5)和LINQ和Server 2008 R2。
如果通过调试模式运行该函数,则此函数将始终返回true,但如果不进行调试,则返回false。我曾经得到过一个错误:http://i.imgur.com/HydhT.png
我试着搜索这个,出现了一些类似的问题,但我检查了所有这些:
毫无办法。我以前从未经历过这样的事情。有人知道吗?
代码:
Server中的facebookID字段为varchar(50) NULL,默认值为NULL
public static bool changeFacebookIDByEmail(string email, string facebookID)
{
UserProfile profile = (from s in _dc.Users
join u in _dc.Memberships on s.UserId equals u.UserId
join i in _dc.UserProfiles on u.UserId equals i.userID
where u.Email == email
select i).SingleOrDefault();
profile.facebookID = facebookID;
ChangeSet cs = _dc.GetChangeSet();
_dc.SubmitChanges();
if (cs.Updates.Count <= 0)
return false;
else
return true;
}
发布于 2012-09-16 03:37:16
似乎您正在执行一个手动SQL语句:
UPDATE UserProfiles SET facebookID = NULL WHERE userID = '85A6D951-15C8-4892-B17D-BD93F3D0ACBB'
这将将facebookID
设置为空。但是,实体框架不知道这一点。它不能解释原始SQL。因此,它仍然认为facebookID
被设置为某种值。当您稍后在changeFacebookIDByEmail
中将其设置为该值时,EF认为没有什么改变。
Sou您可能不应该执行原始SQL字符串。使用EF将值更改为null。
发布于 2012-09-15 14:23:14
将.singleordefault改为.single,我认为您的问题很快就会暴露出来。
基本上,在数据文本中使用be查询可以找到任何内容。任何新事物(如违约)。
您需要重写您的例程,以便在找不到新用户的情况下插入它。
编辑
这是我一直在做的一个项目的一些代码。我已经剪掉了一堆东西来演示它应该如何工作。
// add the identity as necessary
var found = dB.Identities.SingleOrDefault(q => q.UserName == userName);
if (found == null)
{
found = new Identity
{
Domain = domain,
Password = User.Identity.AuthenticationType,
Salt = "",
UserName = userName,
LastUpdatedDateTime = DateTime.Now
};
dB.Identities.InsertOnSubmit(found);
}
dB.SubmitChanges(null);
https://stackoverflow.com/questions/12438194
复制相似问题