none
ObjectContext scope question: Can anyone help me with saving changes with ASP.NET and Entity Framework 3.5? RRS feed

  • Question

  • Project Background

    I have my EntityModel, class structure, business logic, and web controls built to present the data to the user in an ASP.NET website. I'm dynamically rendering web user controls within Repeaters. The web control has a few properties that get set for each item in my List<EntityClass>, then a collection of web controls gets dropped into a Repeater which gets added to my web page. Simple.

    The Problem

    My ObjectContext is out of scope at the time the SaveButton_Click event fires.

    The user should be able to perform CRUD operations using this interface, then press the "Save" button to have everything committed to database. I'm still somewhat new to this methodology, and the traditional way would be to simply iterate over my collection of web user controls, then update the corresponding database records with their properties using a stored procedure and ADO.NET. Ideally, I would be able to reference my ObjectContext.SaveChanges() static method on the SaveButton_Click event. 

    I created a Console Application to retrieve, modify, create, and save data using the same Entity Model. It works as I would expect, but I know that's because the ObjectContext's scope is valid inside of my Console Application.

    The Question

    What is your recommended approach for Updating my database with the changes the user has made with these dynamically rendered controls?

    Thursday, June 9, 2011 6:33 PM

Answers

  • On 6/9/2011 2:33 PM, amidoingthiswrongorwhat wrote:
    > *Project Background*
    >
    > I have my EntityModel, class structure, business logic, and web controls
    > built to present the data to the user in an ASP.NET website. I'm
    > dynamically rendering web user controls within Repeaters. The web
    > control has a few properties that get set for each item in my
    > List<EntityClass>, then a collection of web controls gets dropped into a
    > Repeater which gets added to my web page. Simple.
    >
    > *The Problem*
    >
    > My ObjectContext is out of scope at the time the SaveButton_Click event
    > fires.
    >
    > The user should be able to perform CRUD operations using this interface,
    > then press the "Save" button to have everything committed to database.
    > I'm still somewhat new to this methodology, and the traditional way
    > would be to simply iterate over my collection of web user controls, then
    > update the corresponding database records with their properties using a
    > stored procedure and ADO.NET. Ideally, I would be able to reference my
    > ObjectContext.SaveChanges() static method on the SaveButton_Click event.
    >
    > I created a Console Application to retrieve, modify, create, and save
    > data using the same Entity Model. It works as I would expect, but I know
    > that's because the ObjectContext's scope is valid inside of my Console
    > Application.
    >
    > *The Question*
    >
    > What is your recommended approach for Updating my database with the
    > changes the user has made with these dynamically rendered controls?
    >
     
    Well IMO, you shouldn't be trying to save the objects on the front-end.
    You should be using DTO(s) that were mapped from the entities to
    populate the user control. The user control works with the properties of
    the DTO and populates the DTO at the UI. The  DTO has state of IsNew,
    IsDirty and IsDeleted set by the actions at the UI.
     
    1) IsNew and IsDirty the DTO is to be inserted in the database.
    2) !Isnew and IsDirty the DTO is to be updated in the database.
    3) IsDelete the DTO is to be deleted from the database.
     
    You send the DTO(s) back to the DAL through the BLL. It's the DAL that
    has EF awareness and scope.
     
    If #1,  the DTO is mapped back to the entity and the entity is inserted
    into the database.
     
    If #2, the DTO is mapped back to entity and the entity is updated on the
    database.
     
    If #3,  then take the key in the DTO and delete the entity from the
    database.
     
    Your Save button only initiates the action of sending the DTO or DTO(s)
    back to the DAL where the data is persisted using EF.
     
    Friday, June 10, 2011 3:07 AM