Employeebusiness ens = brand-new EmployeeService();Cityorganization cs = brand-new CityService();... Do not take a parameter in ns constructor, ns guesns the girlfriend develop a conmessage within ns classes. As soon as you loAD the city1...

You are watching: An entity object cannot be referenced by multiple instances of ientitychangetracker.

Payroll.Entities.City city1 = cs.SelectCity(...);...girlfriend connect ns city1 to the context in CityService. Later on you include a city1 as a referral to ns brand-new Employee e1 and include e1 including thins recommendation to city1 to ns context in EmployeeService. Together an outcome you have city1 attached to two various context which is wcap ns exemption comlevels about.

girlfriend have the right to solve thins by developing a context external that ns organization classens and injecting and also using it in both services:

Employeeorganization es = brand-new EmployeeService(context);Citybusiness cs = brand-new CityService(context); // same conmessage instanceher company classens look at a little bit prefer repositoriens which are responsible for just a single reality type. In such a situation girlfriend will constantly have actually problem together shortly as relationship between entities to be affiliated as soon as you use separate contexts for the services.

girlfriend deserve to additionally produce a solitary company i beg your pardon ins responsible for a set of closely related entities, prefer a EmployeeCityservice (i m sorry has a single context) and also delegate ns totality procedure in your Button1_Click technique come an approach the thins service.

See more: Ramada Rockville Centre Rockville Centre Ny, Ramada By Wyndham Rockville Centre Hotel

measures to reproduce can be streamlined come this:

var contextone = new EntityContext();var context2 = new EntityContext();var user = contextOne.Users.FirstOrDefault();var group = new Group();group.User = user;contextTwo.Groups.Add(group);contextTwo.SaveChanges();password withthe end error:

var context = new EntityContext();var user = context.Users.FirstOrDefault();var group = brand-new Group();group.User = user; // it is in cautious once girlfriend collection reality properties. // it is in sure that all objectns come from ns same contextcontext.Groups.Add(group);context.SaveChanges();making use of just one EntityConmessage have the right to solve this. Describe various other answerns for various other solutions.

This ins a old thread, but one more solution, i beg your pardon i prefer, ins simply update ns cityid and also not assign the feet model City come Employee... Come execute the Employee must watch like:

publicly class Employee ... Windy int? CityId; //ns ? is for allow City nullmay be windy digital City City;climate it"s sufficient assigning:

C# is the Main problemLeibniz golf in C#code Injectitop top functions in C# too!Shortest Hello people program via no semi colonsUpday .network webwebsite without reloadingdeserve to CLR execution continue after 'THROW' ins encountered in T-SQL?setting up a central CLR save procedure / feature respository library because that internal stored procns in various other databases to use?day-to-day database maintain using SQtogether Server 2008 and also a save on computer procedurethird party dll in SQl Server CLRLogin to SQl Server making use of windows Authentication