SaveChangesAsync is not working for different contexts
See original GitHub issueThis SavecCangesAsync method is not convenient for multiple different contexts beacuse of casting issue.
Startup
services.AddUnitOfWork<BlogContext, BlogLogContext>();
Service
public class UserService : IUserService
{
private readonly IUnitOfWork<BlogContext> _blogUnitOfWork;
private readonly IUnitOfWork<BlogLogContext> _blogLogUnitOfWork;
public UserService(IUnitOfWork<BlogContext> blogUnitOfWork, IUnitOfWork<BlogLogContext> blogLogUnitOfWork) {
_blogUnitOfWork = blogUnitOfWork;
_blogLogUnitOfWork = blogLogUnitOfWork;
}
public async Task<bool> DeleteAuthorWithEntireStuff(int id)
{
try
{
var userRepo = _blogUnitOfWork.GetRepository<User>();
var postRepo = _blogUnitOfWork.GetRepository<Post>();
var commentRepo = _blogUnitOfWork.GetRepository<Comment>();
var logRepo = _blogLogUnitOfWork.GetRepository<Log>();
var postList = postRepo.GetAll().Where(p => p.AuthorId == id).ToList();
var commentList = commentRepo.GetAll().Where(c => c.UserId == id).ToList();
userRepo.Delete(id);
commentRepo.Delete(commentList);
postRepo.Delete(postList);
logRepo.Create(new Log
{
Message = $"All stuff of userId:{ id } has been deleted",
Level = "Information",
MessageTemplate = "Information",
TimeStamp = DateTime.UtcNow.ToString(),
Exception = null,
LogEvent = null,
Properties = null
});
// or await vice versa
return await _blogUnitOfWork.SaveChangesAsync(_blogLogUnitOfWork)!=0;
}
catch (Exception)
{
throw;
}
}
}
UnitOfWork
foreach (var item in unitOfWorks)
{
var uow = item as UnitOfWork<DbContext>;
uow.Context.Database.UseTransaction(tran.GetDbTransaction());
count += await uow.SaveChangesAsync();
}
Here when I call SaveChangesAsync in UnitOfWork<BlogContext>, as TContext is BlogContext casting from BlogContext to BlogLogContext is not available, also from DbContext to BlogLogContext is not available as well.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
SaveChangesAsync not updating value in database table
SaveChangesAsync will kick off the IO command and then free up the request thread to do other work while the IO is in...
Read more >SaveChangesAsync is not Working .Without ...
This happens if you are using the same dbcontext object into another functions and doing savechanges. If you want to limit the dbcontext...
Read more >EF: When SaveChanges does not save changes
I was debugging an issue with DbContext.SaveChanges which was neither saving changes to the database nor throwing any exceptions, ...
Read more >SaveChangesAsync does not save changes made in the ...
SaveChangesAsync does not save changes made in the context to the database. Exception message: System.InvalidOperationException: The connection ...
Read more >SaveChangesAsync reutrns 0 although Properties have ...
I am wondering if it is possible that EF Core's SaveChangesAsync() returns 0, even though properties have been changed? For context I use ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@fabercs The v2.1.0 had fixed this issues
I known what problem now, I’ll fix it today.