1

我正在使用实体框架来更新两个表。数据库模式由具有自动递增 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)
4

2 回答 2

0

我不是 100% 肯定的,因为我只使用 Code First 完成了 Entity,但我认为您在 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 上的虚拟修饰符。

于 2012-07-13T14:37:37.510 回答
0

尝试改变

dbCustomer.LinesOfBusinesses = tempCustomer.LinesOfBusinesses;

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

让我们知道例外情况仍然存在。我认为您不能对导航属性 ( dbCustomer.LinesOfBusinesses) 产生任何影响。您可以添加、删除、查询对象。但是您不能修改引用的实例。

于 2012-07-13T15:07:26.250 回答