Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faking foreign key updates using entity keys

In Alex James' Entity Framework tips articles, (which are excellent by the way) he talks about how to fake foreign key properties. This seems like exactly what I need, but for whatever reason I can't seem to pull it off when I'm updating. I have the following in update portion of my controller:

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(candidates candidateToEdit)
    {
        string EduIDValue = candidateToEdit.education.ID.ToString();

        if (!ModelState.IsValid)
            return View();

        try
        {
            var originalCandidate = (from c
                                            in model.candidates
                                            where c.ID == candidateToEdit.ID select c).FirstOrDefault();

            //attempting it here
            originalCandidate.educationReference.EntityKey = new System.Data.EntityKey("model.education", "ID", candidateToEdit.education.ID);

            model.ApplyPropertyChanges(originalCandidate.EntityKey.EntitySetName, candidateToEdit);                
            model.SaveChanges();
            return RedirectToAction("Index");
        }
        catch(Exception e)
        {
            Response.Write("Education ID Value " + EduIDValue + "<br /><br />Error: <br /><br />" + e);
            return null;                
            //return View();
        }
    }

This fails and spits out the following:

System.ArgumentException: The member with identity 'model' does not exist in the metadata collection. Parameter name: identity at System.Data.Metadata.Edm.MetadataCollection1.GetValue(String identity, Boolean ignoreCase) at System.Data.Metadata.Edm.ReadOnlyMetadataCollection1.GetValue(String identity, Boolean ignoreCase) at System.Data.Metadata.Edm.ItemCollection.GetEntityContainer(String name, Boolean ignoreCase) at System.Data.Metadata.Edm.ItemCollection.GetEntityContainer(String name) at System.Data.Metadata.Edm.MetadataWorkspace.GetEntityContainer(String name, DataSpace dataSpace) at System.Data.EntityKey.GetEntitySet(MetadataWorkspace metadataWorkspace) at System.Data.Objects.DataClasses.EntityReference.set_EntityKey(EntityKey value) at InternshipTest.Controllers.CandidatesController.Edit(candidates candidateToEdit) in C:\Documents and Settings\graham\My Documents\Visual Studio 2008\Projects\InternshipTest\InternshipTest\Controllers\CandidatesController.cs:line 84

Which doesn't make any sense to me, model is definitely the name of the EntitySet.

like image 575
Graham Conzett Avatar asked Jul 08 '09 19:07

Graham Conzett


1 Answers

I figured it out, I did in fact have the EntitySet incorrect. I got around it by passing the following as the first argument when creating a new entity key:

originalCandidate.educationReference.EntityKey = new System.Data.EntityKey(originalCandidate.educationReference.EntityKey.EntityContainerName + "." + originalCandidate.educationReference.EntityKey.EntitySetName, "ID", candidateToEdit.education.ID);

Kinda nasty, but it works. Looking forward to the foreign key mess in EF being sorted out in .net 4.0

like image 89
Graham Conzett Avatar answered Oct 02 '22 02:10

Graham Conzett