我正在使用实体框架来更新两个表。数据库模式由具有自动递增 ID 的 Customer 表和具有外键关系的子表组成。子表只有两列 - 客户 ID 的复合键和作为 varchar 中第二列的信息。
我有一个视图对象:
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();
}
}
我对 Entity Framework 很陌生:我哪里出错了?
谢谢!
编辑
应评论的要求,这里是调用 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)