Entity Framework Hooking tools
C#
Latest commit 717d354 Feb 13, 2014 @kmckelvin Update README.markdown
Permalink
Failed to load latest commit information.
EFHooks.Tests Add overload to allow for specification of database name or connectio… Aug 11, 2011
EFHooks Add overload to allow for specification of database name or connectio… Aug 11, 2011
packages Created Action Hooks and Metadata Jun 20, 2011
.gitignore Setup project skeleton Jun 20, 2011
EFHooks.sln
LICENSE.txt Added MS-PL License Jun 22, 2011
README.markdown Update README.markdown Feb 13, 2014

README.markdown

EFHooks

This repo is no longer maintained since I've left the Microsoft/C# environment. Use visoft's fork instead: https://github.com/visoft/EFHooks.

EFHooks is a framework to assist in hooking into the Entity Framework Code First before and after insert, update and delete actions are performed on the database.

EFHooks is designed to lend itself to code that is easy to unit test with the least amount of mocking possible and without cluttering up your DbContext class with hooking code. It also is designed to play well with IoC containers.

Getting Started:

Define a hook to fire before an action by deriving from one of the strongly typed hook classes: PreInsertHook<TEntity>, PreUpdateHook<TEntity> or PreDeleteHook<TEntity> and override the Hook method. (There are also Post-Action hooks)

The example below will automatically set the CreatedAt property to DateTime.Now

public class TimestampPreInsertHook : PreInsertHook<ITimeStamped>
{
    public override void Hook(ITimeStamped entity, HookEntityMetadata metadata)
    {
        entity.CreatedAt = DateTime.Now;
    }
}

Then derive your DbContext from the EFHooks.HookedDbContext and register the hooks.

public class AppContext : HookedDbContext
{
    public AppContext() : base()
    {
        this.RegisterHook(new TimestampPreInsertHook());
    }

    public DbSet<User> Users { get; set; }
    public DbSet<Post> Posts { get; set; }
}

New up the AppContext and your hooks are in place and will fire when you call SaveChanges();