c# - Adding EF object as a foreign key -


OK, I'm pulling my hair on it for a few days.

How to insert an object in an object instead of an entirely new object for an existing object? I've tried all kinds of things I'm getting the error "Object object manager has an object with the same key already. ObjectSet Manager can not track many objects with the same key."

I am just trying to add a country to an address. Countries are listed in a separate database table, the country comes from a dropdown list posted from the MVC view, so I have only ID, this means that I can get the only way I can query the eF which results in Duplicate object is there.

Hope someone can help. Crave!

M

=== More details ===

The above points only tell the country ID very well to set up, it is only for construction Works Scenes It does not work to edit.

I have this in my controller:

  Edit Public Functionality (SiteViewModel siteViewModel) {if (ModelState.IsValid) {site site = _unitOfWork.SiteRepository.GetById ( SiteViewModel.SiteID); _unitOfWork.Detach (site); Site = mapper Map & lt; Site will appear, Site & gt; (SiteVodel); Site. Country ID = View siteModel.County; ...}}   

I still get many objects with the same key error

Please help!

M

Try:

  var id = 2; // item id (var reference = new context ()) {var item = context.YourTable.Single (m => m.id == ID); Item.ForeinKey = someNewForeinKey; Context.SubmitChanges (); }    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -