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 246555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:10:56+00:00 2026-05-11T21:10:56+00:00

I am new to LINQToSQL. Is there a way to overwrite the InsertOnSubmit or

  • 0

I am new to LINQToSQL. Is there a way to overwrite the “InsertOnSubmit” or “DeleteOnSubmit” methods of the DataContext class for a particular entity?

Like for example, I have a database table called Customers that has a boolean field “IsDeleted” which holds true if the user deletes a customer record from UI. If I will call the _myDataContext.Customers.DeleteOnSubmit(..), bydefault it will physically delete the record from the table which I don’t want. Instead, I want it to be logically deleted by setting the “IsDeleted” field to true.

One way of doing this is to Get the object and call the Update method (instead of Delete) after setting the property value. This will work as well but just out of curiosity, I want to know if the standard DataContext methods (InsertOnSubmit, DeleteOnSubmit etc) are over-writable? And if so, how?

Thanks

  • 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-11T21:10:57+00:00Added an answer on May 11, 2026 at 9:10 pm

    As far as I know, no they are not. If you don’t want to delete it… don’t call delete! However, some other options:

    • you could map the delete to a stored procedure that just sets the flag at the database
    • you may be able to override SubmitChanges and fix-up any changes (via GetChangetSet) before calling base.SubmitChanges – however, I’m dubious as to whether this is a good idea; it may need re-inserting the item, for example.

    Code:

    partial class MyDataContext {
        public override void SubmitChanges(ConflictMode failureMode) {
            var delta = GetChangeSet();
            foreach(var record in delta.Deletes.OfType<Customer>()) {
                Customers.InsertOnSubmit(record);
                record.IsDeleted = true;
            }
            base.SubmitChanges(failureMode);
        }
    }
    

    Obviously if it needs to be more flexible you may want to use GetTable() (rather than a rigid Customers property).


    Updates re your comment; I honestly don’t think you can do it at that point; re the “10 places” thing… IMO you should be hiding the data-context behind a repository interface anyway, so all 10 places would be calling a method like CreateUser which deals with the data-context and necessary logic (perhaps with a separate business logic class to handle some of the rules). And further, uniqueness should usually be handled at the database level anyway (via a constraint) due to concurrency concerns.

    But to do what you want before trying to save them:

    partial class MyDataContext {
        public override void SubmitChanges(ConflictMode failureMode) {
            var delta = GetChangeSet();
            foreach(var record in delta.Inserts.OfType<User>()) {
                if(Users.Any(x=>x.Name == record.Name) {...throw an exception...}
            }
            base.SubmitChanges(failureMode);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to linqtosql. I have a database schema where, An employee can
I have a winform app that uses LinqToSql as it's DAL. There is a
I have a DataLayer class (LINQ2SQL, not entity framework) that returns rows from the
I have a partial class to extend one of my LinqToSql classes. In this
If I have two tables... Category and Pet. Is there a way in LINQ
I am going to develop a new website with asp.net 3.5 and LinqToSQL. For
New to PHP and MySQL, have heard amazing things about this website from Leo
I have a long LinqtoSQl query in which several parameters I'm not forcing the
i have a table with hierarchical structure. like this: (source: aspalliance.com ) and table
Still new to LINQ2SQL so please forgive my ignorance ... I have one user

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.