2012-07-12 57 views
1

我正在使用實體框架來更新兩個表。數據庫模式由具有自動遞增ID的Customer表和具有外鍵關係的子表組成。子表只有兩列 - 客戶ID的組合鍵和作爲varchar中第二列的信息。更新引發EntityCollection已經初始化錯誤

我有一個視圖對象:

public class CustomerView 
{ 
    public long id { get; set; } 
    public String firstName { get; set; } 
    public String lastName { get; set; } 
    public String streetAddress { get; set; } 
    public String city { get; set; } 
    public String state { get; set; } 
    public String zip { get; set; } 
    public String profession { get; set; } 
    public String[] linesOfBusiness { get; set; } 
} 

這是視圖對象與實體之間我的映射類:

internal static Customer getCustomerFromView(CustomerView customer) 
{ 
    Customer newCustomer = new Customer 
    { 
     Customer_ID = customer.id, 
     FirstName = customer.firstName, 
     LastName = customer.lastName, 
     Street = customer.streetAddress, 
     City = customer.city, 
     State = customer.state, 
     Zip = customer.zip, 
     Profession = customer.profession 
    }; 
    foreach (String line in customer.linesOfBusiness) 
    { 
     newCustomer.LinesOfBusinesses.Add(new LinesOfBusiness { Customer_ID = customer.id, LineOfBusiness = line }); 
    } 
    return newCustomer; 
} 

我可以創建實體就好了,但是當我去更新我遇到的實體集合中的一個已經被初始化錯誤。這裏是更新方法:

private void updateCustomer(CustomerView customer) 
{ 
    using (LocalCustomerDB data = new LocalCustomerDB()) 
    { 
     Customer dbCustomer = (from c in data.Customers where c.Customer_ID == customer.id select c).Single(); 
     Customer tempCustomer = CustomerMapper.getCustomerFromView(customer); 
     dbCustomer.FirstName = tempCustomer.FirstName; 
     dbCustomer.LastName = tempCustomer.LastName; 
     dbCustomer.LinesOfBusinesses = tempCustomer.LinesOfBusinesses; 
     dbCustomer.Profession = tempCustomer.Profession; 
     dbCustomer.State = tempCustomer.State; 
     dbCustomer.Street = tempCustomer.Street; 
     dbCustomer.Zip = tempCustomer.Zip; 
     dbCustomer.City = tempCustomer.City; 
     data.SaveChanges(); 
    } 
} 

我是相當新的實體框架:我哪裏出錯了?

謝謝!

編輯


在這裏評論的請求是調用update()的代碼;

public long saveCustomer(CustomerView customer) 
     { 
      long returnValue = 0; 
      using (LocalCustomerDB data = new LocalCustomerDB()) 
      { 
       if (customer.id > 0) 
       { 
        updateCustomer(customer); 
        returnValue = customer.id; 
       } 
       else 
       { 
        Customer newCustomer = 
        CustomerMapper.getCustomerFromView(customer); 
        data.Customers.AddObject(newCustomer); 
        data.SaveChanges(); 
        returnValue = newCustomer.Customer_ID; 
       } 
      } 
      return returnValue; 
     } 

,這裏是堆棧跟蹤:

at System.Data.Objects.DataClasses.RelationshipManager.InitializeRelatedCollection[TTargetEntity](String relationshipName, String targetRoleName, EntityCollection1 entityCollection) 
    at CustomerRegistry.Customer.set_LinesOfBusinesses(EntityCollection`1 
value) in C:\Users\Michael\Documents\Personal 
Projects\CustomerRegistry\CustomerRegistry\CustomerRegistry\CustomerModel.Designer.cs:line 
386 
    at CustomerRegistry.Project_Code.DataAccess.CustomerRepository.updateCustomer(CustomerView 
customer) in C:\Users\Michael\Documents\Personal 
Projects\CustomerRegistry\CustomerRegistry\CustomerRegistry\Project 
Code\DataAccess\CustomerRepository.cs:line 41 
    at CustomerRegistry.Project_Code.DataAccess.CustomerRepository.saveCustomer(CustomerView 
customer) in C:\Users\Michael\Documents\Personal 
Projects\CustomerRegistry\CustomerRegistry\CustomerRegistry\Project 
Code\DataAccess\CustomerRepository.cs:line 19 
    at CustomerRegistry.CustomerEntry.Submit_Click(Object sender, 
EventArgs e) in C:\Users\Michael\Documents\Personal 
Projects\CustomerRegistry\CustomerRegistry\CustomerRegistry\CustomerEntry.aspx.cs:line 
40 
    at System.Web.UI.WebControls.Button.OnClick(EventArgs e) 
    at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 
    at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String 
eventArgument) 
    at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler 
sourceControl, String eventArgument) 
    at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 
    at System.Web.UI.Page.ProcessRequestMain(Boolean 
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 
+0

顯示您調用'updateCustomer()'的代碼。 – rikitikitik 2012-07-13 03:23:49

+0

請發佈「實體集合已經初始化」的堆棧跟蹤錯誤 – Luxspes 2012-07-13 06:28:35

回答

0

嘗試改變

dbCustomer.LinesOfBusinesses = tempCustomer.LinesOfBusinesses; 

tempCustomer.LinesOfBusinesses.ForEach(z => dbCustomer.LinesOfBusinesses.Add(z)); 

讓我們知道它的例外是還在這裏。 我認爲你不能影響任何導航屬性(dbCustomer.LinesOfBusinesses)。您可以添加,刪除,查詢對象。但是你不能修改引用的實例。

+0

異常消失了,其他一切都會更新。然而,業務線仍然沒有更新。 – user1522221 2012-07-13 19:13:12

+0

在通過lambda表達式運行更新之前,我最終刪除了現有的項目。謝謝一堆。 – user1522221 2012-07-18 23:37:43

0

我不是100%肯定的,因爲我一直在使用的Code First只是做實體,但我認爲你缺少的收集虛擬修改CustomerView中的子對象。嘗試將CustomerView更改爲:

public class CustomerView 
{ 
    public long id { get; set; } 
    public String firstName { get; set; } 
    public String lastName { get; set; } 
    public String streetAddress { get; set; } 
    public String city { get; set; } 
    public String state { get; set; } 
    public String zip { get; set; } 
    public String profession { get; set; } 
    public virtual String[] linesOfBusiness { get; set; } 
} 

注意linesOfBusiness上的虛擬修飾符。