diff --git a/docs/Entity-Framework-Core.md b/docs/Entity-Framework-Core.md new file mode 100644 index 0000000000..c486d4b5b8 --- /dev/null +++ b/docs/Entity-Framework-Core.md @@ -0,0 +1,113 @@ +## Entity Framework Core Integration + +This document explains how to integrate EF Core as ORM provider to ABP based applications and how to configure it in details. + +### Installation + +`Volo.Abp.EntityFrameworkCore` is the main nuget package for EF Core integration. Install it to your project (for a layered application, to your data/infrastructure layer): + +```` +Install-Package Volo.Abp.EntityFrameworkCore +```` + +Then add `AbpEntityFrameworkCoreModule` module dependency to your [module](Module-Development-Basics.md): + +````C# +using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.Modularity; + +namespace MyCompany.MyProject +{ + [DependsOn(typeof(AbpEntityFrameworkCoreModule))] + public class MyModule : AbpModule + { + //... + } +} +```` + +### Creating DbContext + +You can create your DbContext as you normally do. It should be derived from `AbpDbContext` as shown below: + +````C# +using Microsoft.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore; + +namespace MyCompany.MyProject +{ + public class MyDbContext : AbpDbContext + { + //...your DbSet properties + + public MyDbContext(DbContextOptions options) + : base(options) + { + } + } +} +```` + +### Registering DbContext To Dependency Injection + +Use `AddAbpDbContext` method in your module to register your DbContext class for [dependency injection](Dependency-Injection.md) system. + +````C# +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.Modularity; + +namespace MyCompany.MyProject +{ + [DependsOn(typeof(AbpEntityFrameworkCoreModule))] + public class MyModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + services.AddAbpDbContext(); + + //... + } + } +} +```` + +#### Add Default Repositories + +ABP can automatically create repositories (TODO: link) for the entities in your DbContext. Just use `AddDefaultRepositories()` option on registration: + +````C# +services.AddAbpDbContext(options => +{ + options.AddDefaultRepositories(); +}); +```` + +This will create a repository for each aggreate root entity (classes derived from AggregateRoot) by default. If you want to create repositories for other entities too, then set `includeAllEntities` to `true`: + +````C# +services.AddAbpDbContext(options => +{ + options.AddDefaultRepositories(includeAllEntities: true); +}); +```` + +Then you can inject and use `IRepository` or `IQueryableRepository` in your services (see repository documentation - TODO). + +TODO: Example + +#### Add Custom Repositories + +TODO... + +### Advanced Topics + +We will cover advanced techniques especially used to develop reusable modules and modular applications. + +#### Set Base DbContext Class or Interface for Default Repositories + +... + +#### Replace Other Repository + +... \ No newline at end of file diff --git a/docs/Index.md b/docs/Index.md index 6ec2c6da45..370f40179b 100644 --- a/docs/Index.md +++ b/docs/Index.md @@ -12,5 +12,7 @@ * Basics * Plug-In Modules * Best Practices +* Data Access + * [Entity Framework Core Integration](Entity-Framework-Core.md) * Presentation (User Interface) * Localization \ No newline at end of file