Skip to content

Instantly share code, notes, and snippets.

@clausjoergensen
Created March 5, 2012 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clausjoergensen/1981492 to your computer and use it in GitHub Desktop.
Save clausjoergensen/1981492 to your computer and use it in GitHub Desktop.
class Test<PageType> : DbContext
where PageType : class
{
private Dictionary<Type, PageType> callbacks = new Dictionary<Type, PageType>();
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
foreach (var type in callbacks.Keys)
{
var entity = modelBuilder.Entity<Page>();
var callback = this.GetType().GetMethod("InternalMap").MakeGenericMethod(type);
entity.GetType().InvokeMember("Map", BindingFlags.InvokeMethod, null, entity, new object[] { type });
}
}
private void InternalMap<TEntityType>(EntityMappingConfiguration<TEntityType> m)
{
m.Requires("PageTypeId").HasValue(callbacks[typeof(TEntityType)].PageTypeId);
m.MapInheritedProperties();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment