Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8332863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:50:36+00:00 2026-06-09T02:50:36+00:00

I’m having a bit of an… odd issue. I’m getting an error with saving

  • 0

I’m having a bit of an… odd issue. I’m getting an error with saving a newly created entity that has a * to 1 relation with another entity. Bit difficult to explain but here’s the set up:

(Note: I am using MVC 2 – Entity Framework 4)

My entities are set up as follows (names replaced for readability, but the structure is as described):
There are some managers in our company. (tblManagers) Each manager owns an undefined amount of Stores (tblStores). Each store has an undefined amount of Equipment (tblEquipment).
So my DataModel is as follows:

(tblManagers) 1<—–>* (tblStores) 1<——>* (tblEquipment)

This setup is valid, as we already have a lot of data in there (from exporting an old Access file).

When creating a new equipment, you obviously have to select which store it belongs to. The value selected in the dropdownlist (the ID of the Store) is passed to the HttpPost method, which executes the following code:

db is my database context variable

myNewItem.tblStore = db.tblStores.Single(x => x.Id == store_id_from_dropdown);

db.tblEquipments.AddObject(myNewItem);

db.SaveChanges();

Which seems to me to be valid code. Resharper sees no issues, also no compile errors (or even warnings). However, when running the code, I encouter the following exception (thrown on db.SaveChanges()):

Entities in 'CMT_DevEntities.tbl_Stores' participate 
in the 'tblManagerstblStores' relationship. 
0 related 'tblManager' were found. 
1 'tblManager' is expected.

I have no idea why it’s even looking at the tblManager as there is no reference to it in my code. I understand that this has to do with the relationship between managers and their stores, but I dont see why.

Here’s the part that confuses me:

  • When looking at the debugvalues of myNewItem, I can see the store is added to the entity. I can even check the tblManager property of the Store, and it is the correct tblManager. So this confirms everything is OK in the database (Stores couldn’t even exist if they didn’t have a Manager).
  • I’ve actually left out a bit of the code. There is an exactly similar navigation property in the Equipment (let’s say Supplier. The Supplier in turn has navigation property to a Country. The relations are exactly the same). I get no errors for this one though. Only the Store/Manager one.

I’m probably missing something, maybe something quite simple. Either way, I have no idea why this is happening.

So my question is: Given an entity created by the ModelBinder in MVC and an (int) ID for the navigation property, how do I add my new entity to the database?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:50:39+00:00Added an answer on June 9, 2026 at 2:50 am

    Instead of loading all the Store entities, you can add a Foreign Key Id for Store to your Equipment entity (equipment.StoreId).

    This will make sure that EF sees the relation between your newly created Equipment and your Store entity without loading the whole graph.

    Since you are using the Model first approach, you can find the documentation on how to do this here on MSDN.

    The important part is in the Note sections:

    You can choose to not add navigation properties or foreign key
    properties to the entities at the ends of the association by clearing
    the Navigation Property and Add foreign key properties to the Entity checkboxes. If you add only one navigation property,
    the association will be traversable in only one direction. If you add
    no navigation properties, you must choose to add foreign key
    properties in order to access entities at the ends of the association.
    For many-to-many (:) and one-to-one (1:1) associations, you cannot
    add foreign keys to the entities. For more information, see Defining
    and Managing Relationships (Entity Framework).

    You should check the ‘Add foreign key properties to the ‘Equipment’ Entity’ in the Add Association dialog.
    Using Foreign Key properties is a new feature in Entity Framework 4. It will make your live a lot easier.

    Here you can find a more detailed explanation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.