A plugin for Microsoft.EntityFrameworkCore to support repository, unit of work patterns, and multiple database with distributed transaction supported.
In MySQL, physically, a schema is synonymous with a database. You can substitute the keyword SCHEMA instead of DATABASE in MySQL SQL syntax, for example using CREATE SCHEMA instead of CREATE DATABASE. Some other database products draw a distinction. For example, in the Oracle Database product, a schema represents only a part of a database: the tables and other objects owned by a single user.
So, for MySQL, the easy way to support this feature is to dynamically change the SCHEMA at the runtime.
After v1.1.2 had support MySQL multiple databases/tables sharding in the same model in the same machine. For different machine, you can use DbContextFactory to dynamically create DbContext. You can use Pomelo.EntityFrameworkCore.MySql to test this feature. @PomeloFoundation
publicvoidConfigureServices(IServiceCollectionservices){// use in memory for testing.services.AddDbContext<QuickStartContext>(opt =>opt.UseInMemoryDatabase()).AddUnitOfWork<QuickStartContext>();}privatereadonlyIUnitOfWork_unitOfWork;// 1. IRepositoryFactory used for readonly scenario;// 2. IUnitOfWork used for read/write scenario;// 3. IUnitOfWork<TContext> used for multiple databases scenario;publicValuesController(IUnitOfWork unitOfWork){_unitOfWork=unitOfWork;// Change database only work for MySQL right now.unitOfWork.ChangeDatabase($"uow_db_{DateTime.Now.Year}");varuserRepo=unitOfWork.GetRepository<User>();varpostRepo=unitOfWork.GetRepository<Post>();varym=DateTime.Now.ToString("yyyyMM");userRepo.ChangeTable($"user_{ym}");postRepo.ChangeTable($"post_{ym}");varuser=newUser{UserName="rigofunc",Password="password"};userRepo.Insert(user);varpost=newPost{UserId=user.UserId,Content="What a piece of junk!"};postRepo.Insert(post);unitOfWork.SaveChanges();varfind=userRepo.Find(user.UserId);find.Password="p@ssword";unitOfWork.SaveChanges();}