2012-07-12 56 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上的虚拟修饰符。