[英]ado.net entity data model finding a row? then calling an if statment
我在查找SQL表中的记录中是否包含任何内容时遇到了麻烦,而且我不确定我要去哪里。 这是我目前的距离:
using (var context = new SADWorkshopEntities())
{
var query = (from p in context.user_profile
where p.deviceID == deviceid
select p);
if (query == null)
{
context.user_profile.Add(new user_profile()
{
deviceID = deviceid,
uri = subscriberUri
});
context.SaveChanges();
}
添加设备ID和uri的代码可以工作,但是由于我添加了if语句而没有调用它。我试图说是否没有具有相同deviceid的记录,然后创建一个新记录,然后我要尝试添加更新记录查询以获取是否有记录,但是我很挣扎。
谢谢
尝试
using (var context = new SADWorkshopEntities())
{
var query = (from p in context.user_profile
where p.deviceID == deviceid
select p).FirstOrDefault();
if (query == null)
{
context.user_profile.Add(new user_profile()
{
deviceID = deviceid,
uri = subscriberUri
});
context.SaveChanges();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.