Skip to content

EFCore DbContext RepositoryFactory Pattern managed by DbContextFactory

License

Notifications You must be signed in to change notification settings

msx752/SampleDotnet.RepositoryFactory

Repository files navigation

Nuget CodeQL MIT

EFCore DbContext RepositoryFactory Pattern managed by DbContextFactory

EntityFrameworkCore doesn't support multiple parallel operations, when we need parallel actions in different threads such as adding or deleting on the same DbContext, It throws an exception when calling SaveChanges source.

NOTE: DbContext service scope set as Transient which managed by IServiceScopeFactory

How to Use

using SampleDotnet.RepositoryFactory;

ServiceCollection Definition

services.AddDbContextFactoryWithUnitOfWork<UserDbContext>(opt =>
    {
        opt.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
    });

then we call transient scoped DbContext

    public class UserController : Controller
    {
        private readonly IUnitOfWork _unitOfWork;

        public UserController(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        [HttpDelete("{id}")]
        public ActionResult Delete(Guid id)
        {
            using (var repository = _unitOfWork.CreateRepository<UserDbContext>())
            {
                var personal = repository.FirstOrDefault<UserEntity>(f => f.Id == id);

                //some operations goes here....

                repository.Delete(personal);

                //some operations goes here....
            }

            _unitOfWork.SaveChanges();
            return Ok();
        }
    }

Additional Feature

  • If IHasDateTimeOffset interfece used on Entity object then value of the the CreatedAt and UpdatedAt properties will be updated automatically.
        public class TestUserEntity : IHasDateTimeOffset
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public Guid Id { get; set; }
            public string Name { get; set; }
            public string Surname { get; set; }

            public DateTimeOffset? CreatedAt { get; set; }
            public DateTimeOffset? UpdatedAt { get; set; }
        }