Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thliborius/EfCoreNexus
Integrate the entity framework core into your blazor app with ease and less code.
https://github.com/thliborius/EfCoreNexus
blazor entity-framework-core
Last synced: 3 months ago
JSON representation
Integrate the entity framework core into your blazor app with ease and less code.
- Host: GitHub
- URL: https://github.com/thliborius/EfCoreNexus
- Owner: thliborius
- License: mit
- Created: 2024-02-27T16:04:13.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-03-25T14:11:49.000Z (8 months ago)
- Last Synced: 2024-05-15T12:09:05.184Z (6 months ago)
- Topics: blazor, entity-framework-core
- Language: C#
- Homepage:
- Size: 557 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: license.txt
Awesome Lists containing this project
- awesome-blazor - EfCoreNexus - ![GitHub stars](https://img.shields.io/github/stars/thliborius/EfCoreNexus?style=flat-square&cacheSeconds=604800) ![last commit](https://img.shields.io/github/last-commit/thliborius/EfCoreNexus?style=flat-square&cacheSeconds=86400) EfCoreNexus helps integrating the entity framework core into your blazor app. Via reflection it adds your entitiy classes automatically and provides you with basic crud functionality. (Libraries & Extensions / 2D/3D Rendering engines)
README
[![Contributors][contributors-shield]][contributors-url]
[![Forks][forks-shield]][forks-url]
[![Stargazers][stars-shield]][stars-url]
[![Issues][issues-shield]][issues-url]
[![MIT License][license-shield]][license-url]
[![Nuget][nuget-shield]][nuget-url]# EfCoreNexus
Integrate the entity framework core into your blazor app with ease and less code.
## About The Project
**Tired of copy & 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.**
With 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.
## Run the sample app
- Check out the repo.
- Edit the EfCoreNexus.TestApp\EfCoreNexus.TestApp.Data\migrations_connectionstring.bat and adjust the connection string to an empty sql server database.
- Call migrations_add.bat with a migration name as parameter, e.g. "migrations_add.bat Init".
- Call migrations_updatedb.bat.
- Add something to the table test that was created in the database, if you want to see something in the app.
- Open the solution and start the TestApp (just hit F5).## How to use it in your project
These are the steps that are neccessary to hook up the entity framework via EfCoreNexus with your blazor app.
- Add a reference to the EfCoreNexus.Framework library or install the nuget package.
- Add a DbContext and DbContextFactory class, derived from EfCoreNexus base classes BaseContext and BaseContextFactory.
```
public class MainContext(DbContextOptions options, IEnumerable configurations)
: BaseContext(options, configurations)
{
}
```
```
public class MainContextFactory : BaseContextFactory
{
public MainContextFactory()
{
// only necessary when you want to use migrations, see implementation in project
}public MainContextFactory(DataAssemblyConfiguration assemblyConf, DbContextOptionsBuilder optionsBuilder)
: base(assemblyConf, optionsBuilder)
{
}public override MainContext CreateDbContext()
{
return new MainContext(OptionsBuilder.Options, EntityConfigurations);
}
}
```
- Create your entity classes. They must implement the IEntity interface to get instatiated via reflection.
```
public class Test : IEntity
{
[Key]
public Guid TestId { get; set; }public bool Active { get; set; }
}
```- 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.
```
public class TestProvider(TransactionService transactionSvc)
: ProviderBase(transactionSvc)
{
public async Task> GetActiveOrderedByDate()
{
var ctx = await GetContextAsync().ConfigureAwait(false);try
{
var items = GetAllQuery(ctx);
return await items.Where(x => x.Active == true).OrderBy(x => x.CurrentDate).ToListAsync().ConfigureAwait(false);
}
finally
{
if (TransactionSvc is { CtxTransaction: null })
{
await ctx.DisposeAsync().ConfigureAwait(false);
}
}
}
}
```
If you have multiple sql statements you can run them in a transaction:
```
public async Task DeactivateAndCreate(Test newEntity)
{
try
{
TransactionSvc.BeginTransaction();var ctx = await GetContextAsync().ConfigureAwait(false);
var itemsToDeactivate = GetDbSet(ctx).Where(x => x.Active == true);
foreach (var item in itemsToDeactivate)
{
item.Active = false;
}
ctx.Set().UpdateRange(itemsToDeactivate);GetDbSet(ctx).Add(newEntity);
await ctx.SaveChangesAsync().ConfigureAwait(false);
await TransactionSvc.CommitTransaction().ConfigureAwait(false);
}
finally
{
await TransactionSvc.DisposeTransaction().ConfigureAwait(false);
}
}
```- Add a few lines to your startup/program-class to register the context classes in the di container.
```
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
ConfigureDataservice(builder.Services);
...
}private static void ConfigureDataservice(IServiceCollection services)
{
var connectionString = "...";
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseSqlServer(connectionString);var assemblyConf = new DataAssemblyConfiguration("Yournamespace.Data");
var ctxFactory = new MainContextFactory(assemblyConf, connectionString);
var startupConf = new StartupConfiguration(ctxFactory, optionsBuilder);startupConf.ConfigureDataservice(services);
}
```- 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.
```
internal class TestConfiguration : EntityTypeConfigurationDependency
{
public override void Configure(EntityTypeBuilder builder)
{
builder.HasKey(x => x.TestId); // already defined as attribute in entity class, for demonstration purpose
}
}
```- 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.
- The dataservice MainSvc is injected into the IoC container at startup. Access it in your pages via the inject attribute.
```
[Inject]
protected DataService MainSvc { get; set; } = default!;
```Now you are ready to go. Here is an example how to add a database entry and retrieve all of them:
```
var p = MainSvc.GetProvider();var newEntity = new Test
{
TestId = Guid.NewGuid(),
CurrentDate = DateTime.Now,
Content = $"Testapp entry {DateTime.Now:F}",
Active = true
};
await p.Create(newEntity, newEntity.TestId);TestList = await p.GetAllAsync();
```
For detailled information have a look at the sample app in this repo.## Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.
If 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".
Don't forget to give the project a star! Thanks again!## License
Distributed under the MIT License. See `LICENSE.txt` for more information.
## Project
Project Link: [https://github.com/thliborius/EfCoreNexus](https://github.com/thliborius/EfCoreNexus)
[contributors-shield]: https://img.shields.io/github/contributors/thliborius/EfCoreNexus.svg?style=for-the-badge
[contributors-url]: https://github.com/thliborius/EfCoreNexus/graphs/contributors
[forks-shield]: https://img.shields.io/github/forks/thliborius/EfCoreNexus.svg?style=for-the-badge
[forks-url]: https://github.com/thliborius/EfCoreNexus/network/members
[stars-shield]: https://img.shields.io/github/stars/thliborius/EfCoreNexus.svg?style=for-the-badge
[stars-url]: https://github.com/thliborius/EfCoreNexus/stargazers
[issues-shield]: https://img.shields.io/github/issues/thliborius/EfCoreNexus.svg?style=for-the-badge
[issues-url]: https://github.com/thliborius/EfCoreNexus/issues
[license-shield]: https://img.shields.io/github/license/thliborius/EfCoreNexus.svg?style=for-the-badge
[license-url]: https://github.com/thliborius/EfCoreNexus/blob/main/license.txt
[nuget-shield]: https://img.shields.io/nuget/v/EfCoreNexus.Framework?style=for-the-badge
[nuget-url]: https://www.nuget.org/packages/EfCoreNexus.Framework/