实体数据模型 ado.net 查找行?然后调用 if 语句



我在确定 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 语句,因此未调用它。我想说如果没有具有相同设备ID的记录,然后创建一个新记录,然后我将尝试添加一个更新记录查询,以确定是否有记录,但我正在努力。

谢谢

尝试

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();
            }

相关内容

最新更新