{"id":19101827,"url":"https://github.com/arch/UnitOfWork","last_synced_at":"2025-04-18T19:30:59.048Z","repository":{"id":38239851,"uuid":"71104796","full_name":"arch/UnitOfWork","owner":"arch","description":"A plugin for Microsoft.EntityFrameworkCore to support repository, unit of work patterns, multiple database with distributed transaction supported, and MySQL multiple databases/tables sharding supported.","archived":false,"fork":false,"pushed_at":"2023-01-08T07:06:08.000Z","size":142,"stargazers_count":1340,"open_issues_count":36,"forks_count":343,"subscribers_count":97,"default_branch":"master","last_synced_at":"2024-11-04T09:09:00.611Z","etag":null,"topics":["entityframeworkcore","unitofwork"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-17T06:01:48.000Z","updated_at":"2024-11-04T02:39:34.000Z","dependencies_parsed_at":"2023-02-08T05:16:15.899Z","dependency_job_id":null,"html_url":"https://github.com/arch/UnitOfWork","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arch%2FUnitOfWork","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arch%2FUnitOfWork/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arch%2FUnitOfWork/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arch%2FUnitOfWork/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arch","download_url":"https://codeload.github.com/arch/UnitOfWork/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223783178,"owners_count":17201911,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["entityframeworkcore","unitofwork"],"created_at":"2024-11-09T03:53:28.960Z","updated_at":"2025-04-18T19:30:59.042Z","avatar_url":"https://github.com/arch.png","language":"C#","readme":"\n# UnitOfWork\nA plugin for Microsoft.EntityFrameworkCore to support repository, unit of work patterns, and multiple database with distributed transaction supported.\n\n## Support MySQL multiple databases/tables sharding\n\n\u003e 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.\n\nSo, for MySQL, the easy way to support this feature is to dynamically change the SCHEMA at the runtime.\n\nAfter [v1.1.2](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.UnitOfWork/1.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. \nYou can use [Pomelo.EntityFrameworkCore.MySql](https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql) to test this feature. @[PomeloFoundation](https://github.com/PomeloFoundation)\n\n# Quickly start\n\n## How to use UnitOfWork\n\n```csharp\npublic void ConfigureServices(IServiceCollection services)\n{\n    // use in memory for testing.\n    services\n        .AddDbContext\u003cQuickStartContext\u003e(opt =\u003e opt.UseInMemoryDatabase())\n        .AddUnitOfWork\u003cQuickStartContext\u003e()\n        .AddCustomRepository\u003cBlog, CustomBlogRepository\u003e();\n}\n\nprivate readonly IUnitOfWork _unitOfWork;\n\n// 1. IRepositoryFactory used for readonly scenario;\n// 2. IUnitOfWork used for read/write scenario;\n// 3. IUnitOfWork\u003cTContext\u003e used for multiple databases scenario;\npublic ValuesController(IUnitOfWork unitOfWork)\n{\n    _unitOfWork = unitOfWork;\n\n    // Change database only work for MySQL right now.\n    unitOfWork.ChangeDatabase($\"uow_db_{DateTime.Now.Year}\");\n\n    var userRepo = unitOfWork.GetRepository\u003cUser\u003e();\n    var postRepo = unitOfWork.GetRepository\u003cPost\u003e();\n\n    var ym = DateTime.Now.ToString(\"yyyyMM\");\n\n    userRepo.ChangeTable($\"user_{ym}\");\n    postRepo.ChangeTable($\"post_{ym}\");\n\n    var user = new User\n    {\n        UserName = \"rigofunc\",\n        Password = \"password\"\n    };\n\n    userRepo.Insert(user);\n\n    var post = new Post\n    {\n        UserId = user.UserId,\n        Content = \"What a piece of junk!\"\n    };\n\n    postRepo.Insert(post);\n\n    unitOfWork.SaveChanges();\n\n    var find = userRepo.Find(user.UserId);\n\n    find.Password = \"p@ssword\";\n\n    unitOfWork.SaveChanges();\n\n    // projection\n\n}\n```\n\n## Projection \u0026 Including\n\n```csharp\n// projection\nvar pagedList = _unitOfWork.GetRepository\u003cBlog\u003e().GetPagedList(b =\u003e new { Name = b.Title, Link = b.Url }, pageIndex: pageIndex, pageSize: pageSize);\nvar projection = _unitOfWork.GetRepository\u003cBlog\u003e().GetFirstOrDefault(b =\u003e new { Name = b.Title, Link = b.Url }, predicate: x =\u003e x.Title.Contains(term));\n\n// including\n[HttpGet]\npublic async Task\u003cIPagedList\u003cBlog\u003e\u003e Get()\n{\n    return await _unitOfWork.GetRepository\u003cBlog\u003e().GetPagedListAsync(include: source =\u003e source.Include(blog =\u003e blog.Posts).ThenInclude(post =\u003e post.Comments));\n}\n```\n","funding_links":[],"categories":["others","C\\#","C# #"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farch%2FUnitOfWork","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farch%2FUnitOfWork","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farch%2FUnitOfWork/lists"}