How to use EntityDataSource in Entity Framework with One-ObjectContext-Per-Request? RRS feed

  • Question

  • I won't lie/hide, I posted exactly this question on the StackOverflow site but after a couple of days I have the feeling I won't get my answer there.

    Original question:

    Here is a copy of it: 

    I just implemented One-ObjectContext-Per-Request in my web project. ( and

    Now I have an issue with EntityDataSource that I used in my asp pages to feed my DetailsView.

    A common problem I’m having is that I have a GridView that is DataBound in my code behind (using my One-ObjectContext-Per-Request).

    My DetailsView has an EntityDataSource (with WhereParameters pointing to the GridView).

    If I delete an entity using my DetailsView, the context of that EntityDataSource is not the same as my One-ObjectContext-Per-Request so my GridView shows my entity is still there. Now in one-ObjectContext-Per-Request I should not need to “refresh” it so I tried making sure my EntityDataSource used my One-ObjectContext-Per-Request:

    protected void ProgramsDatasource_OnContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e)
    .Context = LinQProvider.Entities;
    protected void ProgramsDatasource_OnContextDisposing(object sender, EntityDataSourceContextDisposingEventArgs e)
    .Cancel = true;

    But I get another weird problem now:

    “AcceptChanges cannot continue because the object’s key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.”

    • edit below

    On my "DetailsViewPrograms_OnItemDeleted" event, I tried to "SaveChanges()" on my context. I get a more precise error message.

    {"Cannot insert the value NULL into column 'name', table 'XXXXX.dbo.program'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}

    I know my name column doesn't allow nulls, but that's besides the point, why is my code trying to insert a new entity when I'm trying to delete one...

    • end edit

    So! Here are some questions:

    1. Is it a good idea to impose my One-ObjectContext-Per-Request to my EntityDataSource like that? (It will probably generate a lot of code for my project in my code behind, I'll probably end up wrapping the EntityDataSource.)
    2. Maybe I should stop using EntityDataSource and do my DataBinding only in code behind to manage it manually so my controls use my One-ObjectContext-Per-Request?
    3. What's going on with the error message I'm getting? I tried leaving the EntityDataSource alone and doing a "hard-refresh" on my One-ObjectContext-Per-Request, then my GridView would update correctly with no errors.

    Sorry for the length and thanks!

    So there you go, anyone with experience using EntityDataSource can look into this?



    • Moved by Jackie-Sun Saturday, April 2, 2011 5:35 AM (From:ADO.NET Entity Framework and LINQ to Entities)
    Monday, March 28, 2011 2:27 PM

All replies