在Alex James' Entity Framework tips articles(这是很好的方法)他谈到how to fake foreign key properties。这看起来正是我所需要的,但无论出于何种原因,我在更新时似乎无法将其取消。我在控制器的更新部分如下:使用实体密钥伪造外键更新
[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();
}
}
这种失败,吐出来的是以下几点:
System.ArgumentException:具有标识“模式”的成员没有元数据集合中存在。在System.Data.MetadataCollection的参数名称:identity。在System.Data.Objects.DataClasses的System.Data.EntityKey.GetEntitySet(MetadataWorkspace metadataWorkspace)上的System.Data.Metadata.Edm.MetadataWorkspace.GetEntityContainer(String name,DataSpace dataSpace)上的.Edm.ItemCollection.GetEntityContainer(String name)。在C:\ Documents and Settings \ Graham \ My Documents \ Visual Studio 2008 \ Projects \ InternshipTest \ InternshipTest \ Controllers \ CandidatesController.cs中的InternshipTest.Controllers.CandidatesController.Edit(候选人候选编辑)中的EntityReference.set_EntityKey(EntityKey值):line 84
这对我来说没有任何意义,型号绝对是名字EntitySet。
很高兴你喜欢这个系列。顺便说一句,在我刚刚修复的博客文章中有一个NULL引用异常错误。 – 2009-07-09 04:14:56