2010-08-14 24 views
0

我設置了一對多關係。 (例如有很多電話號碼的人)。在我得到的查詢我有this.ObjectContext.Person.Include("PhoneNumbers")和在生成的元數據包括public EntityCollection<PhoneNumbers> PhoneNumbers{ get; set; }我也建立了一個DTO與這個和我需要的其他屬性。將Entity插入EntityCollection w/RIA

[Include] 
[Association("Name","thisKey","otherKey")] 
public IEnumerable<PhoneNumbers> PNums { get; set; } 

我可以檢索所有數據,並將其顯示在silverlight中,但是當我創建一個新的問題時,我會遇到問題。我有這種事情怎麼回事:

private void Button_Click(object sender, System.Windows.RoutedEventArgs e) 
{ 

    if (dgMMs.SelectedItem != null) 
    { 
     PhoneNumbers wb = new PhoneNumbers(); 
     wb.this = tbThis.Text; 
     wb.that = tbThat.Text; 
     wb.other = tbOther.Text; 
     wb.whatnot = tbwhatnot.Text; 
     ((Person)dgMMs.SelectedItem).PNums.Add(wb); 
    } 
} 

然後調用TDataSource.SubmitChanges();,當我得到這個錯誤:

Message = "Submit operation failed validation. Please inspect Entity.ValidationErrors for each entity in EntitiesInError for more information."

好了,所以我這樣做,果然有錯誤,但我不太明白爲什麼會有。我在數據庫中有一個不可空的字段,當我創建它並將其添加到entityCollection時,我沒有設置這個字段,我想這會導致它,但我的問題來自爲什麼RIA不在我創建的服務中調用我的Insert方法,因爲我想在那裏設置該字段。像這樣:

public void InsertPhoneNumber(PhoneNumbers pnum) 
{ 
    pnum.last_modified = DateTime.Today; 
    pnum.last_modified_by = Thread.CurrentPrincipal.Identity.Name; 
    if ((pnum.EntityState != EntityState.Detached)) 
    { 
     this.ObjectContext.ObjectStateManager.ChangeObjectState(pnum, EntityState.Added); 
    } 
    else 
    { 
     this.ObjectContext.PhoneNumbers.AddObject(pnum); 
    } 
} 

但它就像RIA添加我的對象並調用它自己的插入方法。所以,我用它推出的第一,只是設置屬性了在UI的話,那就給我這個錯誤:

Message = "Submit operation failed. An error occurred while updating the entries. See the inner exception for details. Inner exception message: Cannot insert explicit value for identity column in table 'iset_trkr_writeback' when IDENTITY_INSERT is set to OFF."

我從來沒有設置標識字段的任何東西,我想RIA會爲我做這。但是,當我調試並看看,它的值爲0。但至少這次它在我的服務中調用了我的插入方法......也許我錯過了我的過程中的一件大事,但我真的可以使用一些幫助。謝謝:)

回答

0

你使用Entity Framework?如果是這樣,您的元數據中至少需要一個字段的[Key]屬性。或者創建一個身份/ PK列(int/guid),然後更新元數據。

+0

感謝rob,是的,我所需要做的就是更新刷新元數據。 X_X – Mike 2010-08-17 14:59:10

相關問題