This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
????
Question :
Why I can`t use BulkInsert method to insert all IList entities in my table but if I use Update method I can insert these entities with success ????
-----------------------------------------------------------------------------
Problem is :
I try to insert a IList of Entities using the following method:
1) first way but without success:
public bool InsertTubesList(IList tubesList, Sn_comenzi comanda, out string nrcda)
{
IDbTransaction trans = null;
nrcda = string.Empty;
if (tubesList.Count == 0) return false;
trans = UnitOfWorkHolder.StartTransaction(); //Begin transaction
nrcda = (String.IsNullOrEmpty(comanda.Nrcda)) ? (comanda.Nrcda = new Sn_comenzi_Controller().GetNrDoc(2, true)) : comanda.Nrcda;
int counter = 0;
for (int i = 0; i
![]() ![]() ![]() ![]() ![]() ![]() |
|
|
It looks like the BulkInsert method is not adding the entities to the unit of work (or at least not until *after* you call SaveChanges). SaveChanges will only save entities that are part of the UOW, which requires one of the following: * The entity was originally loaded from the database through the UOW * The entity is a new entity and you have called IUnitOfWork.Add(entity) for it * The entity is a new entity and it has been associated with another entity that is part of the UOW, for example by being added to a children collection * The entity came from another UOW (e.g. in a previous Web request) and you have called IUnitOfWork.Attach(entity) to associate it with the current UOW. Try changing your BulkInsert method to look something like this: public void BulkInsert(IList entities) Or just add each entity to the UOW as you create it. |
|
|
Just to add the the above... if you stick with a BulkInsert method as opposed to adding as you go, and your list contains a mix of new and existing entities, then you will need to add an if (entity.EntityState == EntityState.New) guard clause around UnitOfWork.Add, just as in your Update method. |
|