Anthbs
Anthbs

Reputation: 184

Can you update the database using a LINQ to SQL Entity?

Using LINQ to SQL in WCF I have a update record methods which ATM I manually assign each updated property. Eg.

public void UpdateMachineRecord(Guid SessionID, int MachineRecordID, MachineRecord machineRecord)
{
    if (!ValidSession(SessionID))
        return;

    using (MMHLINQSQLDataContext database = new MMHLINQSQLDataContext())
    {
        MachineRecord record = database.MachineRecords.Single(mr => mr.Job.OperationID == MachineRecordID);
        record.ChangedBy = UserSession(SessionID).Name;
        record.ChangedDate = DateTime.Now.ToString();
        record.Date = machineRecord.Date;
        record.EDI = machineRecord.EDI;
        ...
        database.SubmitChanges();
    }
}

My questions is: Is there a way to update the record using an entire entity?

Eg.

public void UpdateMachineRecord(Guid SessionID, int MachineRecordID, MachineRecord machineRecord)
{
    if (!ValidSession(SessionID))
        return;

    using (MMHLINQSQLDataContext database = new MMHLINQSQLDataContext())
    {
        MachineRecord record = database.MachineRecords.Single(mr => mr.Job.OperationID == MachineRecordID);
        record = machineRecord;
        database.SubmitChanges();
    }
}

Upvotes: 2

Views: 459

Answers (1)

scartag
scartag

Reputation: 17680

You can do this.

database.MachineRecords.Attach(machineRecord, true);
database.SubmitChanges();

This will attach it as a modified entity (that's what the boolean parameter is for)

Upvotes: 3

Related Questions