Tip 42 – How to create a dynamic model using Code-Only

Background:

When we give examples of how to use Code-Only we always start with a strongly typed Context derived from ObjectContext. This class is used to bootstrap the model.

For example this (property bodies omitted for simplicity sake):

public class MyContext : ObjectContext
{
public ObjectSet<Category> Categories { get; }
public ObjectSet<Product> Products { get; }
}

Tells CodeOnly that to bootstrap a model with 2 EntitySets, a set of Category entities called Categories, and a set of Product entities called Products.

Then if necessary you can further refine the model by manipulating the ContextBuilder.

Problem:

But what if you don’t have a strongly typed Context class?

What if you make a determination at runtime that you need a model, there isn't an appropriate strongly typed Context class lying around.

A customer asked me this very question earlier today.

Solution:

It turns out you can use ObjectContext directly. When you do this though Code-Only knows nothing about the model. But that isn’t that bad all you need to do is explicitly tell Code-Only about all the things it would normally learn from the strongly typed context.

For example this:

public ObjectSet<Person> People { get; }

Can be converted into this:

var builder = new ContextBuilder<ObjectContext>();
builder.RegisterSet<Person>(“People”);

Nifty huh?

End to end example:

This example, persists a Person (BillG) to the database and retrieves it again, all without a strongly typed ObjectContext:

First the Person class (which is POCO):

public class Person
{
public int ID { get; set; }
public string Firstname { get; set; }
public string Surname { get; set; }
}

And now the code to setup the ObjectContext:

// Create the contextbuilder, and tell it about the People set.
var builder = new ContextBuilder<ObjectContext>();
builder.RegisterSet<Person>("People");

// Create a connection
string connstr = @"Data Source=.\SQLEXPRESS;Initial Catalog=PeopleDb;Integrated Security=True;Pooling=False;MultipleActiveResultSets=True";
var conn = new SqlConnection(connstr);

// Create an ObjectContext from the builder
using (ObjectContext ctx = builder.Create(conn))
{
// Create the database if it doesn’t already exist
if (!ctx.DatabaseExists())
ctx.CreateDatabase();

// Create Bill
Person p = new Person {
ID = 1,
Firstname = "Bill",
Surname = "Gates"
};

// Add Bill to the context
// UPDATE: thanks to danny for the simplification
ctx.CreateObjectSet<Person>().AddObject(p);
 using the general purpose
// AddObject method.
// The only tricky part is the EntitySet name with must
// be qualified with the the container name,
// in this case is ObjectContext.
ctx.AddObject("ObjectContext.People", p);

ctx.SaveChanges();

    // Issue a query against the People set.
var bill = (from person in ctx.CreateObjectSet<Person>()
where person.Firstname == "Bill"
select person).Single();

    // Make and Save a change.
bill.Firstname = "William";
ctx.SaveChanges();
}

Pretty easy considering it isn’t strongly typed.