Your comment shows important information. When you add this AModel from your combobox to your BModel, it will be disconnected from your DbContext. When you add it to your model, the Entity Framework will think that you have a new object.
Since you have identifiers configured as DatabaseGenerationOptions.None, it will use the primary key that you provide to yourself. In your case, this is a PC of a separate object. Thus, when EF tries to insert this record, it will throw the above exception because an object with this key is already present.
There are several ways to solve this problem:
This object will be attached to your context after retrieval, which allows you to use it. This means an additional search: first, to get them in combobox, and then use the Id from the object in combobox to get it again from the database.
Usage example:
AModel Get(AModel detachedModel) { using(var context = new MyContext()) { return context.AModels.Single(x => x.ID == detachedModel.ID); } }
This should simply make Entity-Framework know that the object already exists in the database.
using(var context = new MyContext()) { context.AModels.Attach(detachedModel); }
Other connection methods - setting the state to "No change"
context.Entry(detachedModel).State = EntityState.Unchanged;
or Modified (in case you also changed the values)
context.Entry(detachedModel).State = EntityState.Modified;
source share