Sharing HBD.EFCore.Extentions

Nuget package

PM> Install-Package HBD.EntityFrameworkCore.Extensions -Version 1.0.0

Introduction

  1. Define the Entities
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id{get;set;}

public string FullName => $"{FirstName} {LastName}";

[Required]
[MaxLength(256)]
public string FirstName { get; set; }

[Required]
[MaxLength(256)]
public string LastName { get; set; }
}
  1. Define Mappers
internal class UserMapper : IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> builder)
{
builder.HasIndex(u => u.FirstName);
}
}
  1. Define DbContext and add the Mapper in.
public partial class MyContext : Microsoft.EntityFrameworkCore.DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new UserMapper());
}
}

Let see, if we have a hundred Entities, we need to do all steps above a hundred times and the third step is an important step as without applying the configuration to the DbContext we are not able to use those entities.
To speed up the development process and cut down the manual works, I developed this extension for EF Core which allows to scan and apply the IEntityTypeConfiguration from assemblies to DbContext automatically.

How HBD.EntityFrameworkCore.Extensions Works.

1. Generic Entity Type Configuration

Sample code: Define a generic Entity Type Configuration

//1. Define BaseEntity
//The recommendation, you should define a BaseEntity on your project which includes all basic properties which will be applied for all entities on the project.
//The Extension provides a few generic class which help you to do the things faster.
public abstract class BaseEntity: Entity {}

//2. Define Generic Entity Type Configuration
internal class MyEntityTypeConfiguration<TEntity> : EntityTypeConfiguration<TEntity> where TEntity: BaseEntity
{
public void Map(EntityTypeBuilder<TEntity> builder){ /*Apply the configuration here for generic entities*/ }
}

//3. Switch from Microsoft.EntityFrameworkCore.DbContext to HBD.EntityFrameworkCore.Extensions.DbContext
public partial class MyContext : Microsoft.EntityFrameworkCore.DbContext
{
//No need to override the OnModelCreating(ModelBuilder modelBuilder)
}

//4. Apply the configuration when App Start
//Normally, the DbContext will be registered with Dependency Injection when App start. So below is a sample code to register the assembles to the extensions as well
var db = new MyDbContext(new DbContextOptionsBuilder().RegisterEntities(op=>op.FromAssemblies(typeof(MyDbContext).Assembly)).Options)

Run the application and show the result, All the entities had been mapped to the DbContext automatically with any further codes.
Now, you want to add new entities? Only entity classes need to be added no more Entity Type Configuration, no more OnModelCreating mapping both had been automated.

2. Specific Entity Type Configuration

//1. Specific Configuration for User Entity
internal class UserTypeConfiguration : EntityTypeConfiguration<User>
{
public void Map(EntityTypeBuilder<User> builder){ /*Apply the configuration here for the User entity*/ }
}

That’s all, You just need to define the specific EntityTypeConfiguration for those special entities. The extension is smart enough to scan and all them to the DbContext and obviously, it will apply the Generic Entity Type Configuration to those others entities.

3. Static Data Loading

//1. Define the Data Seeding
internal class AccountDataSeeding: IDataSeedingConfiguration<Account>{
public TEntity[] Data => new [
new Account {UserName = "admin", Password="123"}
];
}

Recommendation

  1. The Mapper type SHOULD be an implement of IEntityTypeConfiguration<>
  2. Your DbContext SHOULD be from an implement of HBD.EntityFrameworkCore.Extensions.DbContext

Source Code

Hope the library useful.
drunkcoding.net

Originally published at Drunk Coding.

Lernt what, share that

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store