使用linq to sql更改不保存到数据库 [英] Changes not saving to database using linq to sql

查看:179
本文介绍了使用linq to sql更改不保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





这是我的代码:

Hi,

This is my code :

public List<notificationdata> ReadNotification(int memberId, int NotificationId)
       {
           using (KiwiDbContext context=new KiwiDbContext(_ConnectionString))
           {
               NotificationData nd = new NotificationData();
               nd.IsRead = true;

               var query = (from n in context.Notifications where n.MemberID == memberId && n.NotificationID == NotificationId && n.IsRead == false select new NotificationData { NotificationID = n.NotificationID, Message = n.Message, CreatedTime = n.NotificationTime, IsRead = nd.IsRead });
               return query.ToList();


               context.SaveChanges();
           }





执行已完成但未保存到数据库表。



请给我正确的代码。



谢谢



Execution is done but not saving to database table.

Please give me correct code for that.

Thanks

推荐答案

作为您发布的代码,它只是从数据库读取数据而不是将数据保存到数据库。要保存数据,您必须编码如



As your posted code it is just reading the data from database not saving the data to database. To Save data you have to code like

public void AddNotification(string msg, bool readable)
       {
           using (KiwiDbContext context=new KiwiDbContext(_ConnectionString))
           {
               NotificationData nd = new NotificationData();
               nd.IsRead = readable; 
               nd.Message = msg;
               nd.CreatedTime = DateTime.Now;
               context.Notifications.Add(nd);              
               context.SubmitChanges();
           }
        }


这篇关于使用linq to sql更改不保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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