I'm making a transaction in my code and I get unreacheable code detected.Can someone help me to correct it?trans.Commit can't be reached,finding it hard to find solution.
trans.Commit()
What I have tried:
public
async
Task<bool> Save(
string
company,
int
number,
string
registrationNumber)
using
(
var
trans = _dbContext.Database.BeginTransaction())
var
db =
new
SibaCiidDbContext();
var
dbSet = _dbContext.Set<IntermediaryAssignment>();
var
check =
await
(
from
s
in
db.StickerDistributions
join
i
in
db.IntermediaryAssignment
on s.CompanyCode equals i.CompanyCode
where
s.Dispatched ==
false
&& s.CompanyCode ==
company <pre>&& s.StickerCode != i.StickerCode
select
s).ToListAsync();
var
datas =
await
(
from
s
in
db.StickerDistributions
where
s.Dispatched ==
false
&& s.CompanyCode ==
company && s.IntermediaryDispatched ==
false
select
s)
.ToListAsync();
var
data =
await
(
from
s
in
db.StickerDistributions
where
s.Dispatched ==
false
&& s.CompanyCode == company &&
s.IntermediaryDispatched ==
false
select
s).Take(number).ToListAsync();
var
intermediary = (
await
_repo.FindBy(s => s.RegistrationNumber ==
registrationNumber && s.Status == EntityStatus.Active)).FirstOrDefault();
foreach
(
var
sticker
in
data)
if
(dbSet.Any(s => s.StickerCode != sticker.StickerCode))
var
entity =
new
IntermediaryAssignment();
entity.CompanyCode = sticker.CompanyCode;
entity.StickerCode = sticker.StickerCode;
entity.RegistrationNumber = intermediary.RegistrationNumber;
entity.Status = EntityStatus.Active;
entity.CreatedDate = DateTime.Now;
entity.Dispatched =
false
;
entity.IntermediaryType = intermediary.IntermediaryType;
sticker.IntermediaryDispatched =
true
;
dbSet.Add(entity);
return
await
_dbContext.SaveChangesAsync() >
0
;
trans.Commit();
That's because you use the keyword
return
before
trans.Commit()
. That line will never be called.
What you can do is something like this:
var
result =
await
_dbContext.SaveChangesAsync();
trans.Commit();
if
(result >
0
)
return
true
;
return
false
;
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.