question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SaveChangesAsync is not working for different contexts

See original GitHub issue

https://github.com/Arch/UnitOfWork/blob/594a381dce2f6c55904967a6ea10b5a92ed0aeb7/src/Microsoft.EntityFrameworkCore.UnitOfWork/UnitOfWork.cs#L155

This 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:closed
  • Created 5 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
xytingcommented, Sep 7, 2018

@fabercs The v2.1.0 had fixed this issues

1reaction
xytingcommented, Sep 6, 2018

I known what problem now, I’ll fix it today.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found