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

    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:

    http://stackoverflow.com/questions/5433766/how-to-use-entitydatasource-in-entity-framework-with-one-objectcontext-per-reques

    Here is a copy of it: 

    I just implemented One-ObjectContext-Per-Request in my web project. (http://csharpquestions.com/entity-framework/scope-of-entity-framework-object-context-in-asp-net-websites and http://dotnetslackers.com/articles/ado_net/Managing-Entity-Framework-ObjectContext-lifespan-and-scope-in-n-layered-ASP-NET-applications.aspx)

    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)
    {
        e
    .Context = LinQProvider.Entities;
    }
    protected void ProgramsDatasource_OnContextDisposing(object sender, EntityDataSourceContextDisposingEventArgs e)
    {
        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?

    Thanks!

     

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

All replies