How to use DbContext for EntityDatasource in EF 6.1.1

1k Views Asked by At

I have an ASP.NET Web Forms application which uses .NET framework v4.5 and EF v6.1.1.

I am using new EntityDatasource control for binding and updating the <asp:FormView />. Previously, I was using the ObjectContext and now I changed it to use DbContext. After this change I got build errors due to invalid type cast:

e.Context = new SchoolEntities();

This is obvious as the "Context" property of the EntityDatasource EventArgs e is of type ObjectContext while SchoolEntities of type DbContext.

I'm also doing some CRUD operation using the e.Context property e.g:

var school = e.Context.Schools.FirstOrDefault();
e.Context.Departments.Add(...);
e.Context.SaveChanges();

To fix the error I got the ObjectContext from the DbContext as follows:

e.Context = ((IObjectContextAdapter)_ctx).ObjectContext;

Here e is Microsoft.AspNet.EntityDataSource's event args.

Also I have added the EntityDataSource_ContextCreating() event to solve the type casting problem:

protected void EntityDataSource_ContextCreating(object sender, Microsoft.AspNet.EntityDataSource.EntityDataSourceContextCreatingEventArgs e)
{
    if (_ctx == null)
        _ctx = new SchoolEntities();
    e.Context = ((IObjectContextAdapter)_ctx).ObjectContext;
}

After doing this change, I got rid of the build errors but it caused several other errors.

Problems

  1. If I'm using e.Context = ((IObjectContextAdapter)_ctx).ObjectContext; then when getting the entity back(var school = (School)e.Entity;) in the EntityDatasource' updating event I'm losing the navigation properties.

  2. I'm not finding any way how I can avoid the use of ObjectContext in EntityDatasource. I want to stay away from ObjectContext as it is completely removed in EF 7.

What I have tried

So to resolve all these problems, I currently added a page level context object like:

private SchooEntities _ctx;

I removed e.Context = ((IObjectContextAdapter)_ctx).ObjectContext; from the EntityDatasource's context creating event. I also removed the existing codes which was using the e.Context property for doing the CRUD operations. I used the page level context(_ctx) variable instead of e.Context and everything worked fine. So this confirms the culprit is e.Context = ((IObjectContextAdapter)_ctx).ObjectContext;

Questions

  1. How effectively use DbContext with the EntityDatasource (http://www.nuget.org/packages/Microsoft.AspNet.EntityDataSource/) control?

  2. For my case, what changes I will do in order to get back the navigation properties(while retrieving the entity back from the eventargsvar school = (School)e.Entity;)?

  3. If I will not use the e.Context property of the EntityDataSource control then I think there is NO use of EntityDatasource at all? (FYI: I'm using the EntityDatasource control only for Bind and Update operations)

Any help is highly appreciated.

1

There are 1 best solutions below

0
On

To ensure the navigation properties are filled, you need to set those you want populated as a comma separated list in the EntityDataSource's [Include] property.