Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>DataContext</code> should always be short-lived. You should re-factor your design to achieve this.</p> <p>You can consider these points:</p> <ul> <li>Use context per transaction. This way you can make the context short-lived in meaningful way.</li> <li>Rollback when your transaction fails. This includes inserting correct item with new transaction. That means, you have a fresh different context now.</li> <li>Repeat the process until the transaction completes.</li> <li>Again, don't forget to end the context when you are done. This can be easily achieved by making use of the <code>using</code> statement.</li> </ul> <p><a href="http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.aspx" rel="nofollow">Remark from MSDN</a>:</p> <blockquote> <p>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.</p> <p>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.</p> </blockquote> <hr> <p>If it takes time to re-factor your design, temporarily you can do like this:</p> <pre><code> public void InsertList(List&lt;Person&gt; 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; } } </code></pre> <p>and this is how you can call <code>InserList</code> Method:</p> <pre><code> List&lt;Person&gt; liste = new List&lt;Person&gt;(); 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); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload