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
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();
}
}
- 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; }
}