Skip to content

Instantly share code, notes, and snippets.

@billgeek
Last active February 17, 2018 19:17
Show Gist options
  • Save billgeek/772fdc4504b1238feadf8e5e65d3f2fc to your computer and use it in GitHub Desktop.
Save billgeek/772fdc4504b1238feadf8e5e65d3f2fc to your computer and use it in GitHub Desktop.
Creating Entity Framework 6 Fluent API Models using Reflection

Using Reflection and Fluent API to create EF6 Models

Below is an example of how to use reflection and Fluent API to define the models for your DbContext.

Objective

To contain the definition and initialization of a model in a single file and to seperate initialization out of the main context file.

IEntityBuilder.cs

The interface "IEntityBuilder" is primarily used to identify the models that are to be built

User.cs

This is a sample of a model for a "User" object: This class implements IEntityBuilder and therefore the "BuildModel" method.

MyDatabaseContext.cs

A simplified example of the generated Context file showing the required changes.

// Models should implement this Interface to be detected when the OnModelCreating is fired on the Context
using System.Data.Entity;
public interface IEntityBuilder
{
void BuildModel(DbModelBuilder modelBuilder);
}
// This demonstrates the changes required to the default generated DB Context file in your project
using System.Data.Entity;
using System.Data.Common;
using System.Reflection;
public partial class MyDatabaseContext : DbContext
{
public MyDatabaseContext() : base("name=MyDbContext")
{
}
public MyDatabaseContext(DbConnection connection) : base(connection, true)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Here we reflect through the Application to find every class that implements "IEntityBuilder"
// This will allow us to keep the definition and initialization of the object together
var entityBuilderTypes = Assembly.GetExecutingAssembly().GetTypes().Where(asyType => asyType.GetInterfaces().Contains(typeof(IEntityBuilder)));
var typesWithConstructors = entityBuilderTypes.Where(ebType => ebType.GetConstructor(Type.EmptyTypes) != null);
foreach(var type in typesWithConstructors)
{
var createdInstance = Activator.CreateInstance(type) as IEntityBuilder;
createdInstance.BuildModel(modelBuilder);
}
}
}
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
public partial class User : IEntityBuilder
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
// Each model will now have to implement BuildModel which will allow you to define the object using Fluent API
public void BuildModel(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.ToTable("User");
modelBuilder.Entity<User>()
.Property(e => e.UserName)
.HasColumnName("UserName")
.IsUnicode(false)
.HasMaxLength(20)
.IsRequired();
// etc...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment