Search code examples
c#linq-to-sqldelete-record

How can I Update and delete records in my database by LINQ to SQL?


Is there the ability for doing this work? How can I do this work?


Solution

  • var context = new MyDataContext(); var newObj = new User(); newObj.UserID = 1; newObj.Name = "Ted";

    context.Users.InsertOnSubmit(newObj);  //queues for submission
    context.SubmitChanges(); //submits to backend
    

    or for update:

    var context = new MyDataContext();
    var user = context.Users.First(i => i.UserID = 1);
    //entities self aware and automatically synced to database when a value changes
    user.Name = "Dave";
    
    context.SubmitChanges(); //knows about updated record