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

  • Home
  • SEARCH
  • 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 7742773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:21:41+00:00 2026-06-01T09:21:41+00:00

I am rather new to the Entity Framework, so I am probably overlooking something

  • 0

I am rather new to the Entity Framework, so I am probably overlooking something simple here.

In my controller class I am adding a new Category entity to the database, next I am using that entity as a property on a Course entity. When I save the Course entity, the Category is saved to the database AGAIN, while I was hoping the new Course would reference the Category that was already inserted.

The (simplified) controller code that saves the first Category:

// Create and save the category
Category category = new Category {Name = "Test category"};
category = context.Categories.Add(category); 
context.SaveChanges(); //  The category object now has a CategoryId (the pk of the record)

// Create and save the course
Course course = new Course {
  FullDescription = "This is a new course",
  Name = "My new course",
  Category =  category // Hoping this will have EF make a link to the just inserted category
};

context.Courses.Add(course);
context.SaveChanges(); // Saves the Course AND creates a **new** Category in the db

The problem seems to be that I call saveChanges() twice. What works is removing the first call to context.saveChanges(), BUT, this is not my actual code. In my application I use a repository pattern and adding a category is done by calling categoryRepository.AddCategory(Category category). And saving the Course is done in exactly the same way, by calling courseRepo.AddCourse(Course course) that also contains a call to saveChanges().

public Category AddCategory(Category category)
    {
        category = context.Categories.Add(category);
        context.SaveChanges();
        return category;
    }

I don’t want to remove the calls to saveChanges() in AddCourse() and AddCategory(), because I want these to be atomic operations.

I was hoping that returning the category and subsequently using the category as a property on a new Course would link that course to the category, but apparantly that is not the case. How do I link my Course to a category that is already present in 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-01T09:21:42+00:00Added an answer on June 1, 2026 at 9:21 am

    I’m not sure how your data model is structured but you could do something like this.

    course.CategoryId = category.CategoryId;
    

    That way you map the actual foreign key in the relationship and it does the same thing.

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

Sidebar

Related Questions

I am a bit new to LINQ and to Entity Framework, so rather simple
Being new to the Entity Framework, I'm really rather stuck on how to proceed
I'm rather new to WCF and the Entity Framework, so bear with me. I'm
I'm rather new to working with multiple threads in a database (most of my
I'm rather new to MVC and as I'm getting into the whole framework more
I have been using Entity Framework 4.3 on an existing database and I have
I'm using Entity Framework 4 for a simple app and would like to bake
I have extended an Entity Framework 4 entity class with a calculated property in
I have an Entity Framework POCO class that is generated by a T4 Template.
I'm using the Entity Framework to model a simple parent child relationship between 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.