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

后端 未结 5 722
花落未央
花落未央 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:22

    Details at: http://blog.benhall.me.uk/2008/01/custom-insert-logic-with-linq-to-sql.html

    You can override the default update behavior. There are 2 ways of doing this

    The easiest is to create a stored procedure (if you can't do that on your database, the second method should work) which takes the parameters of your customer object and updates the table:

    1. Create the stored procedure that has a parameter for each property of Customers that needs to be updated.
    2. Import that stored procedure into your Linq To SQL DBML file.
    3. Now you can right click on your customers entity and select "Configure Behavior".
    4. Select your Customers class under the Class dropdown and "Update" on the behavior drop down.
    5. Select the "Customize" radio button and choose the stored procedure you just created.
    6. Now you can map class's properties to the stored procedure.

    Now when Linq to SQL tries to update your Customers table, it'll use your stored procedure instead. Just be careful because this will override the update behavior for Customers everywhere.

    The second method is to use partial methods. I haven't actually tried this, so hopefully this might just give you some general direction to pursue. In a partial class for your data context, make a partial method for the update (It'll be Update_____ with whatever your class is in the blank. I'd suggest searching in your data context's designer file to make sure you get the right one)

    public partial SomeDataContext
    {
        partial void UpdateCustomer(Customer instance)
        {
           // this is where you'd do the update, but I'm not sure exactly how it's suppose to work, though. :(
        }
    }
    

提交回复
热议问题