2009-07-08 12 views
6

Trong Alex James' Entity Framework tips articles, (tuyệt vời bằng cách này), ông nói về how to fake foreign key properties. Điều này có vẻ như chính xác những gì tôi cần, nhưng vì lý do gì đó tôi không thể kéo nó ra khi tôi đang cập nhật. Tôi đã điều sau đây trong phần cập nhật của bộ điều khiển của tôi:Thực hiện cập nhật khóa ngoài bằng cách sử dụng các phím tổ chức

[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(); 
     } 
    } 

này thất bại và spits ra như sau:

System.ArgumentException: Các thành viên đậm đà bản sắc 'mô hình' không tồn tại trong bộ sưu tập siêu dữ liệu. Tên tham số: danh tính tại System.Data.Metadata.Edm.MetadataCollection 1.GetValue(String identity, Boolean ignoreCase) at System.Data.Metadata.Edm.ReadOnlyMetadataCollection 1.GetValue (nhận dạng chuỗi, Boolean ignoreCase) tại System.Data.Metadata.Edm.ItemCollection.GetEntityContainer (Tên chuỗi, Boolean ignoreCase) tại System.Data.Metadata .Edm.ItemCollection.GetEntityContainer (Tên chuỗi) tại System.Data.Metadata.Edm.MetadataWorkspace.GetEntityContainer (Tên chuỗi, DataSpace dataSpace) tại System.Data.EntityKey.GetEntitySet (MetadataWorkspace metadataWorkspace) tại System.Data.Objects.DataClasses. EntityReference.set_EntityKey (giá trị EntityKey) tại InternshipTest.Controllers.CandidatesController.Edit (ứng cử viên ứng viênToEdit) trong C: \ Documents and Settings \ graham \ My Documents \ Visual Studio 2008 \ Projects \ InternshipTest \ InternshipTest \ Controllers \ CandidatesController.cs: dòng 84

Điều gì không có ý nghĩa với tôi, mô hình chắc chắn là tên của EntitySet.

Trả lời

4

Tôi đã tìm ra, tôi đã thực tế có EntitySet không chính xác. Tôi nhận được xung quanh nó bằng cách chuyển những điều sau đây dưới dạng đối số đầu tiên khi tạo khóa tổ chức mới:

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

Kinda nasty, nhưng nó hoạt động. Mong đợi những mớ hỗn độn chính ở nước ngoài trong EF được sắp xếp trong .net 4.0

+1

Rất vui khi bạn thích bộ truyện. BTW có một lỗi ngoại lệ tham chiếu NULL trong bài đăng trên blog mà tôi vừa sửa. –