Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8257671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:16:39+00:00 2026-06-08T02:16:39+00:00

I’m working with the Entity Framework to update two tables. The database schema is

  • 0

I’m working with the Entity Framework to update two tables. The database schema is made up of a Customer table with an auto-incrementing ID, and a child table with a foreign key relationship. The child table has only two columns – a composite key of the Customer ID and the information as the second column in a varchar.

I have a view object:

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; }
}

This is my mapping class between the view object and the entity:

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;
}

I can create entities just fine, but when I go to update one I run into the entity collection has already been initialized error. Here is the update method:

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();
    }
}

I’m pretty new to Entity Framework: where have I gone wrong?

Thanks!

Edit


At the request of the comments here is the code that is calling 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;
        }

and here is the stack trace:

 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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:16:41+00:00Added an answer on June 8, 2026 at 2:16 am

    Try to change

    dbCustomer.LinesOfBusinesses = tempCustomer.LinesOfBusinesses;
    

    to

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

    Let us know it the exception is still here.
    I think you can’t affect anything to a navigation property (dbCustomer.LinesOfBusinesses). You can add, remove, query objects. But you can’t modify the referenced instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables with like below codes: Table: Accounts id | username |
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a reasonable size flat file database of text documents mostly saved in
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.