Skip to content

Instantly share code, notes, and snippets.

@andygjp
Created July 11, 2016 20:55
Show Gist options
  • Save andygjp/bf2701cd85a81e85a1d190e1341e3334 to your computer and use it in GitHub Desktop.
Save andygjp/bf2701cd85a81e85a1d190e1341e3334 to your computer and use it in GitHub Desktop.
Automatically add all none generated EntityTypeConfigurations
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{4B7FDFDE-3CFF-4A96-9D76-8B8E495C24DD}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ConsoleApplication1</RootNamespace>
<AssemblyName>ConsoleApplication1</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.3" targetFramework="net452" />
</packages>
namespace ConsoleApplication1
{
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Linq;
using System.Reflection;
class Program
{
static void Main(string[] args)
{
DbModelBuilder modelBuilder = new DbModelBuilder();
ConfigurationHelper.AddCustomConfiguration(modelBuilder.Configurations);
}
}
internal class ConfigurationHelper
{
private static readonly MethodInfo Method;
static ConfigurationHelper()
{
var addMethods = from method in typeof(ConfigurationRegistrar).GetMethods()
where method.Name == nameof(ConfigurationRegistrar.Add)
let @params = method.GetParameters()
where @params.Length == 1
let parameterType = @params.First().ParameterType
where parameterType.IsGenericType
let genericDefinition = parameterType.GetGenericTypeDefinition()
where genericDefinition == typeof(EntityTypeConfiguration<>)
select method;
Method = addMethods.Single();
}
public static void AddCustomConfiguration(ConfigurationRegistrar registrar)
{
var entityTypeConfigurationTypes = CustomEntityTypeConfigurationTypes().ToList();
entityTypeConfigurationTypes.ForEach(t => Add(registrar, t));
}
private static IEnumerable<Type> CustomEntityTypeConfigurationTypes()
{
var types = from type in AssemblyEntityTypeConfigurationTypes()
where type.GetCustomAttribute<GeneratedCodeAttribute>() == null
select type;
return types;
}
private static IEnumerable<Type> AssemblyEntityTypeConfigurationTypes()
{
var types = from type in typeof(ConfigurationHelper).Assembly.DefinedTypes
let baseType = type.BaseType
where baseType?.IsGenericType ?? false
where baseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>)
select type;
return types;
}
private static void Add(ConfigurationRegistrar registrar, Type entityTypeConfigurationType)
{
var baseType = entityTypeConfigurationType.BaseType;
if (baseType?.IsGenericType ?? false)
{
if (baseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>))
{
var entityType = baseType.GenericTypeArguments[0];
AddEntityTypeConfigurationToRegistrar(registrar, entityTypeConfigurationType, entityType);
return;
}
}
throw new ArgumentException($"Argument should be of type {typeof(EntityTypeConfiguration<>).Name}.", nameof(entityTypeConfigurationType));
}
private static void AddEntityTypeConfigurationToRegistrar(ConfigurationRegistrar registrar, Type entityTypeConfigurationType, Type entityType)
{
var instance = Activator.CreateInstance(entityTypeConfigurationType);
var call = Method.MakeGenericMethod(entityType);
call.Invoke(registrar, new[] { instance });
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment