{"id":13807617,"url":"https://github.com/thliborius/EfCoreNexus","last_synced_at":"2025-05-14T00:31:48.320Z","repository":{"id":225146380,"uuid":"764176275","full_name":"thliborius/EfCoreNexus","owner":"thliborius","description":"Integrate the entity framework core into your blazor app with ease and less code.","archived":false,"fork":false,"pushed_at":"2024-03-25T14:11:49.000Z","size":570,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-18T23:53:27.545Z","etag":null,"topics":["blazor","entity-framework-core"],"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/thliborius.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-27T16:04:13.000Z","updated_at":"2024-10-11T13:50:26.000Z","dependencies_parsed_at":"2024-03-04T10:31:48.922Z","dependency_job_id":"257385e2-ef91-46b7-b174-cba23ed31213","html_url":"https://github.com/thliborius/EfCoreNexus","commit_stats":null,"previous_names":["thliborius/efcorenexus"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thliborius%2FEfCoreNexus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thliborius%2FEfCoreNexus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thliborius%2FEfCoreNexus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thliborius%2FEfCoreNexus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thliborius","download_url":"https://codeload.github.com/thliborius/EfCoreNexus/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254046352,"owners_count":22005575,"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":["blazor","entity-framework-core"],"created_at":"2024-08-04T01:01:27.643Z","updated_at":"2025-05-14T00:31:43.290Z","avatar_url":"https://github.com/thliborius.png","language":"C#","funding_links":[],"categories":["Libraries \u0026 Extensions"],"sub_categories":["2D/3D Rendering engines"],"readme":"\u003c!-- PROJECT SHIELDS --\u003e\n\u003c!--\n*** I'm using markdown \"reference style\" links for readability.\n*** Reference links are enclosed in brackets [ ] instead of parentheses ( ).\n*** See the bottom of this document for the declaration of the reference variables\n*** for contributors-url, forks-url, etc. This is an optional, concise syntax you may use.\n*** https://www.markdownguide.org/basic-syntax/#reference-style-links\n--\u003e\n[![Contributors][contributors-shield]][contributors-url]\n[![Forks][forks-shield]][forks-url]\n[![Stargazers][stars-shield]][stars-url]\n[![Issues][issues-shield]][issues-url]\n[![MIT License][license-shield]][license-url]\n[![Nuget][nuget-shield]][nuget-url]\n\n# EfCoreNexus\n\n\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://github.com/thliborius/EfCoreNexus\"\u003e\n    \u003cimg src=\"logo.png\" alt=\"Logo\" width=\"120\" height=\"120\"\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\nIntegrate the entity framework core into your blazor app with ease and less code.\n\n\n## About The Project\n\n**Tired of copy \u0026 pasting a lots of classes when creating a new table and connecting it to your blazor app? EfCoreNexus helps you integrating the entity framework core into your blazor app.**\n\nWith the help of reflection, the entity, the provider and the optional configuration classes getting registered. After it had been set up, it is really easy to add new table to your app. Now you only have to add two classes: the entity class and a provider class, which handle the CRUD operations. Simple operations are covered by the base class while you can add the specific ones. Additionally you can add a configuration for each entity.\n\n\n## Run the sample app\n\n- Check out the repo.\n- Edit the EfCoreNexus.TestApp\\EfCoreNexus.TestApp.Data\\migrations_connectionstring.bat and adjust the connection string to an empty sql server database.\n- Call migrations_add.bat with a migration name as parameter, e.g. \"migrations_add.bat Init\".\n- Call migrations_updatedb.bat.\n- Add something to the table test that was created in the database, if you want to see something in the app.\n- Open the solution and start the TestApp (just hit F5).\n\n## How to use it in your project\n\nThese are the steps that are neccessary to hook up the entity framework via EfCoreNexus with your blazor app.\n\n - Add a reference to the EfCoreNexus.Framework library or install the nuget package.\n\n- Add a DbContext and DbContextFactory class, derived from EfCoreNexus base classes BaseContext and BaseContextFactory.\n```\npublic class MainContext(DbContextOptions\u003cMainContext\u003e options, IEnumerable\u003cEntityTypeConfigurationDependency\u003e configurations) \n\t: BaseContext\u003cMainContext\u003e(options, configurations)\n{\n}\n```\n```\npublic class MainContextFactory : BaseContextFactory\u003cMainContext\u003e\n{\n    public MainContextFactory()\n    {    \n        // only necessary when you want to use migrations, see implementation in project\n    }\n\n    public MainContextFactory(DataAssemblyConfiguration assemblyConf, DbContextOptionsBuilder\u003cMainContext\u003e optionsBuilder)\n        : base(assemblyConf, optionsBuilder)    \n    {\n    }\n\n    public override MainContext CreateDbContext()\n    {\n        return new MainContext(OptionsBuilder.Options, EntityConfigurations);\n    }\n}\n```\n- Create your entity classes. They must implement the IEntity interface to get instatiated via reflection. \n```\npublic class Test : IEntity\n{\n    [Key]\n    public Guid TestId { get; set; }\n\n    public bool Active { get; set; }\n}\n```\n\n- For each entity you need a provider class that must be derived from ProviderBase. The base class will supply you with all the standard CRUD operations (GetAll, GetById, Create, Update, Delete). If you'd like to use special queries you can implement your own queries like the GetActiveOrderedByDate method in the example.\n\n```\npublic class TestProvider(TransactionService\u003cMainContext\u003e transactionSvc) \n\t: ProviderBase\u003cTest, Guid, MainContext\u003e(transactionSvc)\n{\n    public async Task\u003cIList\u003cTest\u003e\u003e GetActiveOrderedByDate()\n    {\n        var ctx = await GetContextAsync().ConfigureAwait(false);\n\n        try\n        {\n            var items = GetAllQuery(ctx);\n            return await items.Where(x =\u003e x.Active == true).OrderBy(x =\u003e x.CurrentDate).ToListAsync().ConfigureAwait(false);\n        }\n        finally\n        {\n            if (TransactionSvc is { CtxTransaction: null })\n            {\n                await ctx.DisposeAsync().ConfigureAwait(false);\n            }\n        }\n    }\n}\n```\nIf you have multiple sql statements you can run them in a transaction:\n```\npublic async Task DeactivateAndCreate(Test newEntity)\n{\n    try\n    {\n        TransactionSvc.BeginTransaction();\n\n        var ctx = await GetContextAsync().ConfigureAwait(false);\n        var itemsToDeactivate = GetDbSet(ctx).Where(x =\u003e x.Active == true);\n        foreach (var item in itemsToDeactivate)\n        {\n            item.Active = false;\n        }\n        ctx.Set\u003cTest\u003e().UpdateRange(itemsToDeactivate);\n\n        GetDbSet(ctx).Add(newEntity);\n\n        await ctx.SaveChangesAsync().ConfigureAwait(false);\n\n        await TransactionSvc.CommitTransaction().ConfigureAwait(false);\n    }\n    finally\n    {\n        await TransactionSvc.DisposeTransaction().ConfigureAwait(false);\n    }\n}\n```\n\n- Add a few lines to your startup/program-class to register the context classes in the di container.\n```\npublic static void Main(string[] args)\n{\n    var builder = WebApplication.CreateBuilder(args);\n    ConfigureDataservice(builder.Services);\n    ...\n}\n\nprivate static void ConfigureDataservice(IServiceCollection services)\n{\n    var connectionString = \"...\";\n    var optionsBuilder = new DbContextOptionsBuilder\u003cMainContext\u003e();\n    optionsBuilder.UseSqlServer(connectionString);\n\n    var assemblyConf = new DataAssemblyConfiguration(\"Yournamespace.Data\");\n    var ctxFactory = new MainContextFactory(assemblyConf, connectionString);\n    var startupConf = new StartupConfiguration\u003cMainContext\u003e(ctxFactory, optionsBuilder);\n\n    startupConf.ConfigureDataservice(services);\n}\n```\n\n- Optional: to configure the entities separately from the entity class you can add configuration classes. They are also automatically instantiated if they derive from EntityTypeConfigurationDependency. \n\n```\ninternal class TestConfiguration : EntityTypeConfigurationDependency\u003cTest\u003e\n{\n    public override void Configure(EntityTypeBuilder\u003cTest\u003e builder)\n    {\n        builder.HasKey(x =\u003e x.TestId);  // already defined as attribute in entity class, for demonstration purpose\n    }\n}\n```\n\n- To use ef core migrations use the batch files provided in the sample app. Don't forget to adjust the batch file that contains the connection string, used as environment variable.\n\n- The dataservice MainSvc is injected into the IoC container at startup. Access it in your pages via the inject attribute.\n```\n    [Inject]\n    protected DataService\u003cMainContext\u003e MainSvc { get; set; } = default!;\n```\n\nNow you are ready to go. Here is an example how to add a database entry and retrieve all of them:\n```\nvar p = MainSvc.GetProvider\u003cTestProvider\u003e();\n\nvar newEntity = new Test\n{\n    TestId = Guid.NewGuid(),\n    CurrentDate = DateTime.Now,\n    Content = $\"Testapp entry {DateTime.Now:F}\",\n    Active = true\n};\nawait p.Create(newEntity, newEntity.TestId);\n\nTestList = await p.GetAllAsync();\n```\nFor detailled information have a look at the sample app in this repo.\n\n## Contributing\n\nContributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.\n\nIf you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag \"enhancement\".\nDon't forget to give the project a star! Thanks again!\n\n## License\n\nDistributed under the MIT License. See `LICENSE.txt` for more information.\n\n## Project\n\nProject Link: [https://github.com/thliborius/EfCoreNexus](https://github.com/thliborius/EfCoreNexus)\n\n\u003c!-- MARKDOWN LINKS \u0026 IMAGES --\u003e\n\u003c!-- https://www.markdownguide.org/basic-syntax/#reference-style-links --\u003e\n[contributors-shield]: https://img.shields.io/github/contributors/thliborius/EfCoreNexus.svg?style=for-the-badge\n[contributors-url]: https://github.com/thliborius/EfCoreNexus/graphs/contributors\n[forks-shield]: https://img.shields.io/github/forks/thliborius/EfCoreNexus.svg?style=for-the-badge\n[forks-url]: https://github.com/thliborius/EfCoreNexus/network/members\n[stars-shield]: https://img.shields.io/github/stars/thliborius/EfCoreNexus.svg?style=for-the-badge\n[stars-url]: https://github.com/thliborius/EfCoreNexus/stargazers\n[issues-shield]: https://img.shields.io/github/issues/thliborius/EfCoreNexus.svg?style=for-the-badge\n[issues-url]: https://github.com/thliborius/EfCoreNexus/issues\n[license-shield]: https://img.shields.io/github/license/thliborius/EfCoreNexus.svg?style=for-the-badge\n[license-url]: https://github.com/thliborius/EfCoreNexus/blob/main/license.txt\n[nuget-shield]: https://img.shields.io/nuget/v/EfCoreNexus.Framework?style=for-the-badge\n[nuget-url]: https://www.nuget.org/packages/EfCoreNexus.Framework/\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthliborius%2FEfCoreNexus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthliborius%2FEfCoreNexus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthliborius%2FEfCoreNexus/lists"}