views:

214

answers:

1

I have a question, much liket this unanswered one. I'm trying to work with the entity framework, and having a tough time getting my foreign tables to update. I have something basically like this in the DB:

Incident (table): -ID -other fields

Responses (table): -FK:Incident.ID -other fields

And and entities that match: Incident (entity) -ID -Other fields -Responses (EntityCollection of Responses via navigation property)

Each Incident can have 0 or more responses.

In my Webpage, I have a form to allow the user to enter all the details of an Incident, including a list of responses. I can add everything to the database when a new Incident is created, however I'm having difficulty with editing the Incident.

When the page loads for edit, I populate the form and then store the responses in the viewstate. When the user changes the list of responses (adds one, deletes one or edits one). I store this back into the viewstate. Then when the user clicks the save button, I'd like to save the changes to the Incident and the Responses back to the DB. I cannot figure out how to get the responses from the detached viewstate into the Incident object so that they can be updated together.

Currently when the user clicks save, I'm getting the Incident to edit from the db, making changes to the Incident's fields and then saving it back to the DB. However I can't figure out how to have the detached list of responses from the viewstate attach to the Incident. I have tried the following without success:

  • Clearning the Incident.Responses collection and adding the ones from the viewstate back in:

    Incident.Responses.Clear()
    for each objResponse in Viewstate("Responses")
    Incident.Responses.add(objResponse)
    next

  • Creating an EntityCollection from my list and then assiging that to the Incident.Responses Incident.Responses = EntityCollectionFromViewstateList

  • Iterating through the responses in Incident.Response and assigning the corresponding object from viewstate:
    for each ObjResponse in Incident.Responses
    objResponse = objCorrespondingModifedResonseFromViewState
    Next

These all fail, I'd like to be able to merge the changes into the Inicdent object so that when the BLL calls SaveChanges on the changes to both the Incident and Responses will happen at the same time.

Any suggestions? I keep finding lots of stuff about assigning foreign keys (singular), but I haven't found a great solution for doing a set of entities assigned to another entity in this manner.

A: 

Did you every find a solution on this? I am having the exact same problem

B to the M
Nope, I found a work around, I don't think it's ideal, but I managed to get it to work... In the end what I did was pull the existing list from the db (the Incident and all the responses, and then compared the 2 lists if the response existed in the DB, then I modified all the fields of the one I pulled from the DB and if it wasn't in the DB, then I added it. If it was in the DB but not in my list, then I marked it for deletion and then did a .saveChanges(). Like I said, it wasn't ideal, but it's working (I'm supposing that it isn't the ideal way to do it, but I'm not sure ;-) )
yougotiger