2009-09-02 60 views
1

我正在嘗試添加一些我創建的實體。當我嘗試將有問題的實體添加到集合中時(見下面的代碼),我得到以下錯誤:EntityReference具有不匹配的EntityKey屬性值?

「該對象無法添加或附加,因爲它的EntityReference具有與EntityKey不匹配的EntityKey屬性值爲這個對象。「

我不知道它指的是什麼entitykey。下面是代碼,有可能是一個更好的方式來退出這個功能,以及:

public Internship CreateInternship(Internship internshipToCreate) 
{ 
    try 
    { 
     Contact contactToCreate = new Contact(); 

     contactToCreate.Fax = internshipToCreate.contacts.Fax; 
     contactToCreate.Extension = internshipToCreate.contacts.Extension; 
     contactToCreate.FirstName = internshipToCreate.contacts.FirstName; 
     contactToCreate.MiddleName = internshipToCreate.contacts.MiddleName; 
     contactToCreate.LastName = internshipToCreate.contacts.LastName; 
     contactToCreate.PhoneNumber = internshipToCreate.contacts.PhoneNumber; 
     contactToCreate.StreetAddress = internshipToCreate.contacts.StreetAddress; 
     contactToCreate.PostalCode = internshipToCreate.contacts.PostalCode; 
     contactToCreate.ContactEmail = internshipToCreate.contacts.ContactEmail; 

     contactToCreate.statesReference.EntityKey = 
        new EntityKey("InternshipEntities.StateSet", "ID", internshipToCreate.contacts.states.ID); 
     contactToCreate.countriesReference.EntityKey = 
        new EntityKey("InternshipEntities.CountrySet", "ID", internshipToCreate.contacts.countries.ID); 

     _internshipEntities.AddToContactSet(contactToCreate); 
     _internshipEntities.SaveChanges(); 

     try 
     { 
      Availability availabilityToCreate = new Availability(); 

      availabilityToCreate.StartDate = internshipToCreate.availability.StartDate; 
      availabilityToCreate.EndDate = internshipToCreate.availability.EndDate; 
      availabilityToCreate.Negotiable = internshipToCreate.availability.Negotiable; 

      _internshipEntities.AddToAvailabilitySet(availabilityToCreate); 
      _internshipEntities.SaveChanges(); 

      try 
      { 
       internshipToCreate.contactsReference.EntityKey = 
        new EntityKey("InternshipEntities.ContactSet", "ID", contactToCreate.ID); 
       internshipToCreate.availabilityReference.EntityKey = 
        new EntityKey("InternshipEntities.AvailabilitySet", "ID", availabilityToCreate.ID); 
       internshipToCreate.classificationsReference.EntityKey = 
        new EntityKey("InternshipEntities.ClassificationSet", "ID", internshipToCreate.classifications.ID); 
       internshipToCreate.educationReference.EntityKey = 
        new EntityKey("InternshipEntities.EducationSet", "ID", internshipToCreate.education.ID); 

       _internshipEntities.AddToInternshipSet(internshipToCreate); //exception here 
       _internshipEntities.SaveChanges(); 

       return internshipToCreate; 
      } 
      catch(Exception e) 
      { 
       throw e; 
      } 
     } 
     catch(Exception e) 
     { 
      throw e; 
     } 
    } 
    catch(Exception e) 
    { 
     throw e; 
    } 

} 

有除了當我經歷了這麼跟蹤我什至不知道哪個鍵是錯誤給出任何其他信息問題。

編輯:這裏是結束了工作的版本:

using (TransactionScope scope = new TransactionScope()) 
{ 
    try 
    { 
     Contact contactToCreate = new Contact(); 
     Availability availabilityToCreate = new Availability(); 
     Internship i = new Internship(); 

     // Set the contact entity values; 

     contactToCreate.Fax = internshipToCreate.contacts.Fax; 
     //... 
     //ommited for brevity 
     //... 
     contactToCreate.ContactEmail = internshipToCreate.contacts.ContactEmail; 

     // Set the contact entity references to existing tables 

     contactToCreate.statesReference.EntityKey = 
        new EntityKey("InternshipEntities.StateSet", "ID", internshipToCreate.contacts.states.ID); 
     contactToCreate.countriesReference.EntityKey = 
        new EntityKey("InternshipEntities.CountrySet", "ID", internshipToCreate.contacts.countries.ID); 

     // Add contact 

     _internshipEntities.AddToContactSet(contactToCreate); 

     // Set the availability entity values; 

     availabilityToCreate.StartDate = internshipToCreate.availability.StartDate; 
     availabilityToCreate.EndDate = internshipToCreate.availability.EndDate; 
     availabilityToCreate.Negotiable = internshipToCreate.availability.Negotiable; 

     // Add availability 

     _internshipEntities.AddToAvailabilitySet(availabilityToCreate); 

     //Add contact and availability entities to new internship entity 

     i.contacts = contactToCreate; 
     i.availability = availabilityToCreate; 

     // Set internship entity values; 

     i.UserID = internshipToCreate.UserID; 
     //... 
     //ommited for brevity 
     //... 
     i.Created = DateTime.Now; 

     // Set the internship entity references to existing tables 

     i.classificationsReference.EntityKey = 
      new EntityKey("InternshipEntities.ClassificationSet", "ID", internshipToCreate.classifications.ID); 
     i.educationReference.EntityKey = 
      new EntityKey("InternshipEntities.EducationSet", "ID", internshipToCreate.education.ID); 

     // Add internship and save 

     _internshipEntities.AddToInternshipSet(i); 
     _internshipEntities.SaveChanges(); 

     //commit transaction 
     scope.Complete(); 

     return internshipToCreate; 

    } 
    catch (Exception e) 
    { 
     throw e; 
    } 
} 
+0

哪個調用SaveChanges會引發錯誤? – 2009-09-02 17:43:55

+0

最內層的try/catch,_internshipEntities.SaveChanges(); – 2009-09-02 17:52:09

回答

2

此代碼是不是賺了很多道理給我。在兩種情況下,只要分配一個對象引用即可通過EntityKey。即,改變這種:

internshipToCreate.contactsReference.EntityKey = 
    new EntityKey("InternshipEntities.ContactSet", "ID", contactToCreate.ID); 
internshipToCreate.availabilityReference.EntityKey = 
    new EntityKey("InternshipEntities.AvailabilitySet", "ID", availabilityToCreate.ID); 

...到:

internshipToCreate.contacts = contactToCreate; 
internshipToCreate.availability = availabilityToCreate; 

在其它兩種情況下,你似乎試圖分配這已經是有對象的ID。這兩條線,即使成功,在我看來,也不會做任何事情:

internshipToCreate.classificationsReference.EntityKey = 
    new EntityKey("InternshipEntities.ClassificationSet", "ID", internshipToCreate.classifications.ID); 
internshipToCreate.educationReference.EntityKey = 
    new EntityKey("InternshipEntities.EducationSet", "ID", internshipToCreate.education.ID); 

所以你可以擺脫它們。

當你做出這兩個改變時會發生什麼?

+0

感謝Craig的迴應。現在很清楚,當我創建實體時,我不需要手動設置這些鍵。你貼 我瘋了變化,我現在得到:通用:更新條目中出現 「錯誤。詳情請參閱的InnerException「 堆棧跟蹤顯示(見下評論):在System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager,IEntityAdapter適配器) 在System.Data.EntityClient.EntityAdapter – 2009-09-02 20:13:50

+0

.Update(IEntityStateManager entityCache) 在System.Data.Objects.ObjectContext.SaveChanges(布爾acceptChangesDuringSave) 在System.Data.Objects.ObjectContext.SaveChanges() 在Internship.Models.EntityInternshipRepository.CreateInternship(實習internshipToCreate)在\項目\ Internship \ InternshipTest \ Models \ EntityInternshipRepository.cs:line 331 – 2009-09-02 20:14:27

+0

InnerException說什麼? – 2009-09-02 20:38:44

3

喂,

雖然我不知道是什麼問題,我有一個建議。您正在傳遞到方法的實習對象(internshipToCreate)用於值傳送到你的方法內實例化其他實體(聯繫人,可用性),和他們創造的作品就好了。

也許你應該嘗試在實習中也這樣做?創建新的實習對象,並通過從internshipToCreate對象中獲取所有值,並將新創建的對象傳遞給_internshipEntities.AddToInternshipSet方法。

這可能是你設置你所需要的其他用途internshipToCreate對象的某些價值觀,其中有些是實際造成的除外。

而且,我不知道你的業務邏輯是什麼,但是如果你把所有的一個事務中,因爲這樣可能會發生創建前兩個實體,而第三個不是會更好。

+0

我現在要試試這個,謝謝你的建議。 – 2009-09-03 14:10:36

+0

謝謝米莎,我用你的建議和克雷格的工作。乾杯! – 2009-09-03 16:34:14

相關問題