2010-11-17 23 views
0

我正在使用EF 4,WCF數據服務和Silverlight 4.我一直從我的服務中獲取數據,沒有任何問題。我試圖用EF中定義的批處理中的實體保存具有非空列的複雜類型。 例如: 客戶擁有複雜的審計類型。審計有EntryDateTime列被標記爲計算(這可能是問題?)。它也有一個地址字段是必需的。在我的Silverlight應用程序中,我有一個表單輸入客戶信息和地址信息。 保存按鈕單擊我做這樣的事情:爲什麼我的WCF數據服務和實體框架4中的SQL插入的值(ComplexType)爲空?

Customer c = new Customer(); 
c.Name = this.nameTextBox.Text; 
// more properties set.... 
Customer.Audit = new Audit(){ UserId = 1, EntryDateTime = DateTime.UtcNow}; 
c.Address = new Address() 
{ 
Address = this.addressTextBox.Text, 
ZipCode = this.zipcodeTextBox.Text, 
// more properties set.... 
Audit = new new Audit(){ UserId = 1, EntryDateTime = DateTime.UtcNow} 
}; 

EDM context = new EDM(new Uri(serviceURL, UriKind.Absolute)); 
// add to the context for the update 
context.AddToCustomers(c); 
context.AddToAddresses(c.Address); 

//我不知道如果更新需要... context.UpdateObject(C); context.UpdateObject(c.Address);

// manually add the link according to http://msdn.microsoft.com/en-us/library/dd756361.aspx, this must be done for the context to know about the relationship (aka association) 
context.SetLink(c, "Address", c.Address); 

// save the changes (batch so all the objects will get created with associations) 
context.BeginSaveChanges(SaveChangeOptions.Batch, new AsyncCallback((iar) => 
{ 
try 
{ 
// Callback method for the async request, retrieves the status of the requested action 
DataServiceResponse response = this.Context.EndSaveChanges(iar); 

// Maps the status of the requested action 

bool status = true; 的foreach(響應ChangeOperationResponse變化) { 如果(change.StatusCode> 200 & & change.StatusCode < 300;!) { 狀態= FALSE; 休息; } } if(!status)拋出新的異常(「存在錯誤的東西」);

我已經設置了斷點並且已經看到了entryDateTime的設置(在EF designer.cs中和saveChanges被調用之前),但是服務器總是拋出一個異常,entryDateTime爲空並且不能爲非數據庫插入的可空列。 我也嘗試附加和更新審計,但這也沒有幫助(如我所料)。

context.AddLink(customer.Audit, "Audit", customer); 
context.UpdateObject(customer.Audit); 

有什麼我失蹤了嗎?

編輯:我昨天晚上很快就提出了這個問題,因爲我正在出去。感謝您的評論,並希望我已經明確提出了我的問題。

+0

該代碼顯然是用於添加/更新對象的內部代碼? (因爲我沒有聽說過'AddLink'或'UpdateObject')。你可以展示這些方法的代碼嗎? – RPM1984 2010-11-17 23:04:17

+0

如果審計是一個複雜的類型,那麼您不能添加一個鏈接到它,因爲它的工作必須是一個實體類型。 – 2010-11-18 14:06:59

+0

您能向我們展示模型(客戶和審計的類定義,還是服務的$元數據輸出)? – 2010-11-18 14:07:27

回答

0

問題是,在Audit Complex Type中,EntryDateTime StoreGeneratedPattern被設置爲Computed,因爲我們試圖讓數據庫自動爲我們設置。我進入數據庫並手動添加GetUtcDate()到默認值或綁定,不再獲得相同的異常。現在的問題是誰在設計師中設置默認日期。

相關問題