Skip to content

Storage Engine: Entity Framework 6

Tomas Lycken edited this page Oct 30, 2017 · 4 revisions

(If you haven't read Getting Started yet, start there and come back here after you've read the intro there.)

Most of the work is already done for you, but there are a few things you'll have to do yourself in order to use RdbmsEventStore with Entity Framework 6.

Create an IEvent implementation for your data model

There is a default implementation as Event<TId, TStreamId> in the library, but since EF6 doesn't support generic entity types, you must create a non-generic subclass for it:

public class YourEventType : Event<Guid, string> { } // or Event<long, long>, etc

The id types can be the same or not, it's entirely up to you. If you want to add metadata, such as the user id of the user who triggered the event, this is where you do that.

Create a DbContext implementation with a DbSet<YourEventType>

This can also inherit directly from the built-in EventStoreContext<TEvent>:

public class YourEventStoreContext : EventStoreContext<YourEventType>
{
    public YourEventStoreContext() : base("name=NameOfYourConnectionString") { }
}

The constructor parameter is just passed along to the underlying DbContext. You can of course add other constructors if you need it for e.g. your design-time tooling; this variant at least works with EF Migrations.

Create the event factory and the context instance

var context = new YourEventStoreContext();
var eventFactory = new EventFactory<Guid, string, YourEventType>();

Create the Event Store

The Entity Framework backing library includes a complete Event Store implementation, so you just need to inject its dependencies and you'll be good to go:

var eventStore = new EntityFrameworkEventStore<Guid, string, YourEventStoreContext, YourEventType>(
    context,
    eventFactory,
    writeLock);

You now have all the pieces in place to start using the Event Store. Go back to Getting Started and keep reading!