Can you convince a DataContext to treat a column as always dirty?

后端 未结 5 731
花落未央
花落未央 2020-12-06 02:39

Is there a way to force LINQ-to-SQL to treat a column as dirty? Globally would suffice....

Basically, I\'ve got a problem with some audit code on a legacy system tha

5条回答
  •  心在旅途
    2020-12-06 03:38

    The following works for me. Note though that I'm using the linq2sql provider from DevArt, but that may not matter:

    MyDataContext dc = new MyDataContext();
    
    Message msg = dc.Messages.Single(m => m.Id == 1);
    Message attachingMsg = new Message();
    attachingMsg.Id = msg.Id;
    
    dc.Messages.Attach(attachingMsg);
    
    attachingMsg.MessageSubject = msg.MessageSubject + " is now changed"; // changed
    attachingMsg.MessageBody = msg.MessageBody; // not changed
    dc.SubmitChanges();
    

    This produces the following sql:

    UPDATE messages SET messageSubject = :p1, messageBody = :p2 WHERE Id = :key1
    

    So, messageBody is updated even though its value is not changed. One other change necessary for this, is that for each property (column) of my entity Message, I have set UpdatedCheck = UpdateCheck.Never, except for its ID, which is the primary key.

提交回复
热议问题