christophano / Effort.Extra

Apache License 2.0
6 stars 3 forks source link

Build status

Extra Effort

Extra Effort is an extension to the excellent Effort library that provides an additional data loader implementation.

Data Loaders

Data Loaders are a method by which you can initialise your database with data already loaded. Effort comes with a couple of data loaders build in (read more on Tamás Flamich's blog). The Extra Effort data loader allows you to create entities in code and add them to strongly typed collections, where each collection represents an Entity Framework set.

Usage

Using the data loader is as simple as passing it to the Effort connection factory methods, as in the example below.

var data = new ObjectData();
// add entities
var dataLoader = new ObjectDataLoader(data);
var connection = DbConnectionFactory.CreateTransient(dataLoader);

Adding entities to the ObjectData instance is as simple as calling the Table method with the generic parameter of the type you want to add. The table name is optional.

data.Table<Person>().Add(new Person { Name = "Jeff" });

If the entity contains a db generated identity field, then this should be provided too. This will enable you to create relations between entities.

data.Table<Person>().Add(new Person { Id = 1, Name = "Jack" });
data.Table<Dog>().Add(new Dog { Id = 1, OwnerId = 1, Name = "Jim" });

If your schema uses a different naming convention that the entity type name, then you can simply provide the set name when calling the Table method.

data.Table<Person>("Folk").Add(new Person { Id = 1, Name = "Jane" });

If your schema calls for multiple sets of the same type, each collection is keyed on the set name, so multiple sets are supported.

data.Table<Person>("SomePeople").Add(new Person { Id = 1, Name = "John" });
data.Table<Person>("OtherPeople").Add(new Person { Id = 1, Name = "June" });