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 6181207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:58:46+00:00 2026-05-24T00:58:46+00:00

I am using EF 4.1 in a MVC 3 project and I am having

  • 0

I am using EF 4.1 in a MVC 3 project and I am having problems with inserting an object. I am using session as well to hold onto some objects. In concrete :

I have a simple class with a parent child relationship:

public class Event
{
    public User Promotor {get;set;}
}

The promotor is based on the CurrentUser. In my application I store the CurrentUser in http session. Now when I add an event like this the user (and all related objects) gets inserted one more time with a new primary key.

        //1st request inserts/loads the user
        User user;
        using (var context = new MyDbContext())
        {
            user = new User();
            context.Users.Add(user);
            context.SaveChanges();
        }

        //2nd request saves the event 
        var before = db.Users.Count();
        var @event = new Event
        {
            Promotor = user, //user was kept in Session
        };
        db.Entry(@event).State = EntityState.Added;
        db.SaveChanges();

When i check the state of the user it is ‘added’ as well although the primary key is not 0 and EF should know it is already persistent. How can fix this without adding a lot of to my persistency code. Do I have to reattach my currentuser to the new dbcontext on every request? This will lead to db code ‘leaking’ into my application. I want to keep the DB stuff in a data layer. I am using a repository like this :

    public void Save(T entity)
    {
        dbContext.Entry(entity).State = IsPersistent(entity) ? 
            EntityState.Modified : EntityState.Added;
        dbContext.SaveChanges();
    }
  • 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-05-24T00:58:49+00:00Added an answer on May 24, 2026 at 12:58 am

    As you’ve already mentioned yourself, reattaching the user to your current context should solve the problem. The only other way I know of, would be to retrieve the object once again in your context based on the primary key value.

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

Sidebar

Related Questions

I am using a Cairngorm MVC architecture for my current project. I have several
I am in the start up of a project using ASP.NET MVC and have
I'm having problems publishing my MVC project. When I do publish and upload everything
I'm having so many problems getting more than 1 MVC project up and running
I'm having problems using the Html.DropDownList helper on a MVC RC1 form. In the
I am using an ASP.NET MVC project and everytime I add a class to
I'm using Linq2Sql for my asp.net mvc project and so far it has worked
I am using windsor DI framework in one of my MVC project. The project
Almost every new Java-web-project is using a modern MVC-framework such as Struts or Spring
I'm starting a project that will be public facing using asp.net mvc. I know

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.