Slowly migrating an EDMX application to use a DbContext?

I have a legacy EF 4 library with a database-first generated ObjectContext with EntityObjects. I'd like to slowly migrate to using DbContext and am in need of some guidence.

One of the overloads for DbContext takes an existing ObjectContext. I thought this would allow me to wrap my existing ObjectContext in a DbContext and expose my existing EntityObjects through IDbSet properties. Unfortunately, when creating the DbContext, the IDbSet properties are not created and instead an exception is thrown with a message of: "Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject."

Is there no way to use a DbContext with IDbSet exposing an existing ObjectContext and EntityObjects? It seems strange that I can create a DbContext with an ObjectContext, but not expose the entities themselves.

Here's some sample code:

public class MyDbContext : DbContext
{
    public MyDbContext(MyObjectContext objectContext, bool dbContextOwnsObjectContext)
        : base(objectContext, dbContextOwnsObjectContext)
    {
    }

    public IDbSet<Person> People { get; set; }
}

The exception is caused by the DbContext trying to create the IDbSet. Person is an EntityObject from my existing EDMX.

Answers


As you noted, you cannot use the existing entity objects that the edmx created.

Have you considered using the Reverse Engineer Code First power tools?

I've used them in several projects, and its simple as pointing it at your database, it generates the model in the same project, and then removing the edmx file.


Need Your Help

Window form with button function powershell

button powershell window

I have this function with "event handler" but I am unable in $event to define value in action section,

Set color of 'empty' area of ListView in Android

android listview background-color

When my list view is not completely full of list view items (ex my list view is tall enough for 8 items, but I only have 2), the empty area shows up as gray on my Droid X. In the emulator the empty...