I asked the question several times several times, and I have not received any answers yet. I try again because I feel that my decision is too confused and I must be missing something simpler.
Using EF 4.1, POCO, DbContext API, AutoMapper and Razor in MVC 3 Application.
I have a many-to-many relationship between my two entities: Suggestions and Category. I can successfully match (Automapper) a sentence with my ProposalViewModel program, including a collection of categories.
In my view, I use javascript to allow the user to add, update and remove tags by dynamically creating elements, each of which stores the identifier of the selected tag.
I can successfully transfer my ViewModel back to my controller, since its CategoryTags collection is full (although only with the ID property for each CategoryTag).
When this ViewModel is sent back to my controller, I do not know how to get these tags from the ViewModel and add them to my model so that db.SaveChanges () correctly updates the database.
The only way I was successful was to disable the CategoryTags collection when matching (by writing them differently), iterate over each tag and manually view it in my context, and then call the .add () method. This is messy for a number of reasons, which is why I think I'm doing it wrong.
Can anyone suggest any direction?
UPDATE:
For anyone interested, my functional code is:
Dim p As New Proposal
Dim tempTag As CategoryTag
p = AutoMapper.Mapper.Map(Of ProposalViewModel, Proposal)(pvm)
db.Proposals.Attach(p)
db.Entry(p).Collection("CategoryTags").Load()
For Each ct In pvm.Tags
tempTag = db.CategoryTags.Find(ct.Id)
If tempTag Is Nothing Then
Continue For
End If
If ct.Tag = "removeMe" Then
p.CategoryTags.Remove(tempTag)
Continue For
End If
p.CategoryTags.Add(tempTag)
Next
db.Entry(p).State = EntityState.Modified
db.SaveChanges()
Return RedirectToAction("Index")