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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:28:05+00:00 2026-06-05T23:28:05+00:00

Here a small piece of code for testing and explain the problem. I have

  • 0

Here a small piece of code for testing and explain the problem. I have a table Person with 3 fields :

  • Id
  • FirstName (not nullable)
  • LastName (not nullable)

In the loop :

  • First : I insert the first row … normal,
  • Second : I try to insert a not correct item, Exception … normal
  • Third : I try to insert the third row … Exception (same than for the second) but the values are correct.

Is there something to do to use the same dataContext after an Exception ?

public class MyTestClass
{
    private readonly DataModelDataContext _dataContext;

    public MyTestClass()
    {
        _dataContext = new DataModelDataContext();
    }

    public void InsertList()
    {
        List<Person> liste = new List<Person>();
        liste.Add(new Person { FirstName = "AAA", LastName = "BBBB" });
        liste.Add(new Person { FirstName = string.Empty, LastName = null });
        liste.Add(new Person { FirstName = "CCC", LastName = "DDD" });

        foreach (var item in liste)
        {
            try
            {
                _dataContext.Persons.InsertOnSubmit(item);
                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
  • 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-06-05T23:28:07+00:00Added an answer on June 5, 2026 at 11:28 pm

    The DataContext should always be short-lived. You should re-factor your design to achieve this.

    You can consider these points:

    • Use context per transaction. This way you can make the context short-lived in meaningful way.
    • Rollback when your transaction fails. This includes inserting correct item with new transaction. That means, you have a fresh different context now.
    • Repeat the process until the transaction completes.
    • Again, don’t forget to end the context when you are done. This can be easily achieved by making use of the using statement.

    Remark from MSDN:

    The DataContext is the source of all entities mapped over a database
    connection. It tracks changes that you made to all retrieved entities
    and maintains an “identity cache” that guarantees that entities
    retrieved more than one time are represented by using the same object
    instance.

    In general, a DataContext instance is designed to last for one “unit
    of work” however your application defines that term. A DataContext is
    lightweight and is not expensive to create. A typical LINQ to SQL
    application creates DataContext instances at method scope or as a
    member of short-lived classes that represent a logical set of related
    database operations.


    If it takes time to re-factor your design, temporarily you can do like this:

        public void InsertList(List<Person> people)
        {
            foreach (var person in people)
            {
                DoInsert(person); 
                // You can use the returned flag and implement the logic if desired.
                // Or let the loop move on its ways.
            }
        }
    
        public bool DoInsert(Person person)
        {
            try
            {
                using (DataModelDataContext dataContext = new DataModelDataContext())
                {
                    dataContext.Persons.InsertOnSubmit(person);
                    dataContext.SubmitChanges();
                }
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }
    

    and this is how you can call InserList Method:

        List<Person> liste = new List<Person>();
        liste.Add(new Person { FirstName = "AAA", LastName = "BBBB" });
        liste.Add(new Person { FirstName = string.Empty, LastName = null });
        liste.Add(new Person { FirstName = "CCC", LastName = "DDD" });
    
        InsertList(liste);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small piece of code here for your consideration which puzzles me
I have a big problem writing a small piece of code using JS/jQuery (don't
here is a small peice off code that i wrote. But I am not
i have a small problem here which i cannot fix,This post goes through but
Here is a small currency converter piece of code: public enum CurrencyType { DOLLAR(1),
Here is a small piece of code. Posted by Russian company Yandex as a
I have a piece of code that divides a image matrix img into small
Here is a small piece of code, the parent process write pipe and child
I have written a small piece of code to calculate quadratic equations, but if
i have written a small piece of code. This code first blocks the {SIGSEGV},

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.