Sabyasachi Mishra
Sabyasachi Mishra

Reputation: 1749

Update columns using entity framework

I want to update some of my columns value using entity framework My code is as follows

public static void UpdateData(string contentMenu, int eid) //Update data in database  
        {
            using (var context = new CmsContext())
            {
                try
                {
                    var objDataContent = new Content
                    {
                        MenuContent = contentMenu,
                        ModifiedDateTime = DateTime.Now
                    };
                   //Code to update only MenuContent and ModifiedDateTime whose id=eid
                    GetRecords();
                }
                catch (Exception ex)
                {
                    //
                }
            }
        }

There are lot of answers in S/O but none of them helped me. I followed https://stackoverflow.com/a/5567616/4701699

How can I update these two columns on the basis of id parameters like MSSQL query

update [CMS Menu].[dbo].[Contents] set MenuContent = 'testing' ,
ModifiedDateTime ='2016-01-02 16:02:15.803' where ContentId=2

Upvotes: 0

Views: 73

Answers (1)

yash
yash

Reputation: 812

You Can Update Your Column Entries by Using Linq:-

Here Are Example Code:-

var data = db.Content.FirstOrDefault(x=>x.ContentId==eID);
data.ModiFiedDateTime = DateTime.Now;
data.MenuContent = contentMenu;
db.SaveChanges();

Upvotes: 2

Related Questions