Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Because these two lines ...</p> <pre><code>EmployeeService es = new EmployeeService(); CityService cs = new CityService(); </code></pre> <p>... don't take a parameter in the constructor, I guess that you create a context within the classes. When you load the <code>city1</code>...</p> <pre><code>Payroll.Entities.City city1 = cs.SelectCity(...); </code></pre> <p>...you attach the <code>city1</code> to the context in <code>CityService</code>. Later you add a <code>city1</code> as a reference to the new <code>Employee</code> <code>e1</code> and add <code>e1</code> <em>including this reference to <code>city1</code></em> to the context in <code>EmployeeService</code>. As a result you have <code>city1</code> attached to two different context which is what the exception complains about.</p> <p>You can fix this by creating a context outside of the service classes and injecting and using it in both services:</p> <pre><code>EmployeeService es = new EmployeeService(context); CityService cs = new CityService(context); // same context instance </code></pre> <p>Your service classes look a bit like repositories which are responsible for only a single entity type. In such a case you will always have trouble as soon as relationships between entities are involved when you use separate contexts for the services.</p> <p>You can also create a single service which is responsible for a set of closely related entities, like an <code>EmployeeCityService</code> (which has a single context) and delegate the whole operation in your <code>Button1_Click</code> method to a method of this service.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload