Skip to content

This project contains the code for some EF6 extensions which make it easier to scan assemblies for entity model types, and for `ComplexTypeConfiguration<>` and `EntityTypeConfiguration<>` types.

License

Notifications You must be signed in to change notification settings

CZEMacLeod/C3D.Core.DataAccess.Extensions.EF6

Repository files navigation

C3D.Core.DataAccess.Extensions.EF6

Build Status NuGet package NuGet downloads

This project contains the code for some EF6 extensions which make it easier to scan assemblies for entity model types, and for ComplexTypeConfiguration<> and EntityTypeConfiguration<> types.

Quick Start

  • Install the Nuget package or copy this code into your project.
  • Ensure your DBContext class has using System.Data.Entity.ModelConfiguration;
  • In your OnModelCreating method use the extension methods
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Configurations.GetTypesFromAssemblyType<C3D.Core.Models.Audit.Conventions.AuditTypesDefaultsConvention>().Add();
}

In this example the generic type is a type from the assembly I want to import from.

There are quite a few overloads/variations and parameters. The code documents some of them.

An example is

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Configurations.AddFromAssembly(typeof(SomeRandomType).Assembly,"My.Random.NameSpace");
}

This will add all CTC and ETC types from the assembly containing SomeRandomType where the configuration type itself or the entity model type it is configuring is in the namespace My.Random.NameSpace.

e.g. this will match both

namespace My.Random.NameSpace.ETC {
    public class MyTypeConfiguration : EntityTypeConfiguration<My.Model.NameSpace.MyType> {

    }
}

and

namespace My.Configuration.NameSpace {
    public class MyTypeConfiguration : EntityTypeConfiguration<My.Random.NameSpace.Models.MyType> {

    }
}

Example

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    List<ITypeInfo> typeInfos = new();
    typeInfos.AddRange(modelBuilder.Configurations.GetTypesFromAssemblyType<C3D.Core.Models.Audit.Conventions.AuditTypesDefaultsConvention>());
    typeInfos.AddRange(modelBuilder.Configurations.GetTypesFromAssemblyType(this.GetType()));
    typeInfos.ForEach(typeInfo => Log.InfoFormat("Detected TC {0}", typeInfo));
    typeInfos.Add();
}

TODO

Better documentation, code documentation and add tests

About

This project contains the code for some EF6 extensions which make it easier to scan assemblies for entity model types, and for `ComplexTypeConfiguration<>` and `EntityTypeConfiguration<>` types.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages