Quantcast
Channel: entityframework Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1793

New Post: Bug or normal work EF 6.1.1?

$
0
0
@popovks: I finally got a few minutes to take a look. Turns out there is actually no key conflict between an instance of Address and an instance of Employee. The conflict is between the two instances of Address.

This line is explicitly making the two addresses have the same key value, which I missed when I first read your post:
address2.Id = address1.Id;
That is not allowed as each entity instance tracked by a DbContext is required to a unique key value. The way to fix it depends on what you were intending to achieve:
  1. If both Employees live in the same Address you should just address1 between the two Employee instances.
  2. If each employee lives in a separate address you should specify a different key value for address2.
Now, if both Employees live in the same address but you are trying to attach a graph of objects that was built somewhere else and contains duplicates, you have several options:
  • Make sure that you always share the same Address instance at the time you construct the graph, i.e. avoid including duplicates in the graph from the beginning
  • Not use instances for this relationships but FK values, i.e. include the AddressId property in the Employee class and set the key values there without setting the Address navigation property.
HTH,
Diego

Viewing all articles
Browse latest Browse all 1793

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>