Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 326131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:16:51+00:00 2026-05-12T09:16:51+00:00

I have a Linq-To-SQL object obj of type MyClass that I have loaded via

  • 0

I have a Linq-To-SQL object obj of type MyClass that I have loaded via my data context.

Now I want to force that object to save, even if no fields have actually changed, so that the save action can set off some triggers behind the scenes.

What’s the easiest way of making my data context think that obj is dirty, so that a call to SubmitChanges() will cause obj to be saved?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:16:52+00:00Added an answer on May 12, 2026 at 9:16 am

    Just change a property to a dummy value and then back…

    var value = obj.SomeField;
    obj.SomeField = "dummy";
    obj.SomeField = value;
    dc.SubmitChanges();
    

    Edit: let me take that back. The L2S change tracker won’t be fooled by that. The easiest/cleanest/safest way if you don’t want to change any of the existing columns is probably to add a new column and change that.

    If you absolutely can’t make any db changes (i.e. add a new column), then going at the change tracker with reflection might be an option. I haven’t tried it, but it looks like the route to that would be (roughly):

    1) the datacontext has a private member called services.
    2) services points to a CommonDataServices which has a private member tracker and an internal member ChangeTracker (returning the former).
    3) Change trackers has a GetTrackedObject internal method that returns a TrackedObject.
    4) TrackedObject has a ConvertToModified method…

    Edit #2: I just tested the reflection route above and it seems to work. E.g.:

                using (advWorksDataContext dc = new advWorksDataContext())
            {
                Employees emp = dc.Employees.FirstOrDefault();
                dc.MakeDirty(emp);
                dc.SubmitChanges();
            }
    

    …and the implementation of MakeDirty is:

    public static class DCExtensions
    {
        internal static void MakeDirty(this System.Data.Linq.DataContext dc, object someEntity)
        {
            //get dc type
            Type dcType = dc.GetType();
            while (dcType != typeof(System.Data.Linq.DataContext))
            {
                dcType = dcType.BaseType;
            }  
    
            //get hold of the CommonDataServices thing in the DC
            System.Reflection.FieldInfo commonDataServicesField 
                = dcType.GetField("services", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            object commonDataServices = commonDataServicesField.GetValue(dc);
            Type commonDataServicesType = commonDataServices.GetType();  
    
            //get hold of the change tracker
            System.Reflection.PropertyInfo changeTrackerProperty 
                = commonDataServicesType.GetProperty("ChangeTracker", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            object changeTracker = changeTrackerProperty.GetValue(commonDataServices, null);
            Type changeTrackerType = changeTracker.GetType();  
    
            //get the tracked object method
            System.Reflection.MethodInfo getTrackedObjectMethod
                = changeTrackerType.GetMethod("GetTrackedObject", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            object trackedObject = getTrackedObjectMethod.Invoke(changeTracker, new object[] { someEntity } );  
    
            //get the ConvertToModified method
            Type trackedObjectType = trackedObject.GetType();
            System.Reflection.MethodInfo convertToModifiedMethod
                = trackedObjectType.GetMethod("ConvertToModified", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);  
    
            //call the convert to modified method
            convertToModifiedMethod.Invoke(trackedObject, null);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a LINQ to SQL query which produces an object of type {System.Data.Linq.DataQuery}.
I have a LINQ TO SQL Context that I have created that calls a
I have a linq-to-sql query that I use to fill an object that has
I have a LINQ to SQL class called Data with a column of type
I have a set of objects that I'm returning via a Linq to SQL
Suppose I have a linq to sql class. I want to return the object
Currently I have a DataModel object which contains my linq to sql classes(a dmbl
I have a class containing Linq To SQL objects that are used to populate
I have a LINQ to SQL DataContext that queries four different tables. But I
I have a linq to sql query that produces duplicates. i can seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.