1
I have this foreach
private void UpdateAzureDiscountGroupReseller(IQueryable<Reseller> model)
{
Reseller resellerObj = new Reseller();
foreach (var item in model)
{
resellerObj.AzureDiscountGroupId = null;
resellerObj.AcceptContractCustomer = item.AcceptContractCustomer;
resellerObj.Id = item.Id;
resellerObj.Name = item.Name;
resellerObj.Alias = item.Alias;
resellerObj.Enabled = item.Enabled;
resellerObj.ResellerMpnId = item.ResellerMpnId;
resellerObj.ServiceApiUser = item.ServiceApiUser;
resellerObj.ServiceApiPassword = item.ServiceApiPassword;
resellerObj.HomeTemplateId = item.HomeTemplateId;
resellerObj.CategoriesIds = item.CategoriesIds;
resellerObj.AcceptContractCustomerId = item.AcceptContractCustomerId;
resellerObj.AcceptContractDate = item.AcceptContractDate;
resellerObj.PathServiceContractAzure = item.PathServiceContractAzure;
resellerObj.PathPartnershipContractAzure = item.PathPartnershipContractAzure;
_resellerService.Update(resellerObj);
}
If I put the call to Update out of foreach, it works. However, I can have more than one Seller with the same FK and what I want is to delete the Group. I can’t give a cascadedelete in the table. Then the solution, to avoid violation of integrity, was, before the Delete in the group, I give an update on FK on Reseller, step to null and then delete. Okay, for a single record that has the FK works fine, but for two or more Reseller’s with the same FK, I need a foreach and in that case gives this error:
New transaction is not allowed because there are other threads running in the Session.
In the Update method it looks like this:
public void Update(Reseller reseller)
{
var existingResellerWithSameAlias = GetByAlias(reseller.Alias);
if (existingResellerWithSameAlias != null && existingResellerWithSameAlias.Id != reseller.Id)
throw new KeyDuplicatedException("There is a reseller with the same ID or Alias on Database");
_resellerRepository.Update(reseller);
cache.Update(reseller.Alias, reseller);
_logTracker.Register(LogTrackType.Update, reseller);
}
And when it gets here it is that the error explodes:
_logTracker.Register(LogTrackType.Update, reseller);
How do I update multiple records at once?
I have doubts about using using with context. How I use using and context and giving a Dispose to kill any open transaction and go to a new one?
– pnet
Trying this, but it’s making this mistake: 'Idbcontext': type used in a using statement must be a implicity Convertible to 'System.Idisposible'. I did it:
using (IDbContext contexto = _context)....
– pnet
I switched to Dbcontext instead of Idbcontext, but it does not accept the new Dbcontext() in using or injecting in the Constructor
– pnet