Note that there are some explanatory texts on larger screens.

plurals
  1. POEF Inserts already existing item
    primarykey
    data
    text
    <p>I am using EF 4.1 Code first for a website that I working on. No I have a small problem that EF tries to insert an item that is already in database, and that is already loaded from the database thru EF.</p> <p>So I can actually see that the Item exist in context, but for some reason it add's the club again and mark it as added to the context. Here is what I am doing.</p> <p>I have 2 entities. Club and User there is a many-to many relation between this to entities. So a Club can have many users and a user can belongs to many clubs.</p> <ul> <li>So I load the club from the database thru EF.</li> <li>Then I create a new User and add the club to my property Clubs on the user class.</li> <li>I then try to save the user thru EF.</li> </ul> <p>Here I get an exception about violating primary key for Club. So it tries to Insert the club into the database when all it should do is add the clubidentifier and the useridentifier to my Club_User relation tables, adn of course save the user...</p> <p>Why does the context think that it's a new club that should be added, when in fact the club already exist in the context?</p> <p>Here is the code regarding club and user</p> <p>First I create the EntityContext and add it to HttpContext.Current.Item</p> <pre><code>protected void Application_BeginRequest(Object sender, EventArgs e) { //-- Create an instance of EntityContext HttpContext.Current.Items[Constants.ENTITYCONTEXT] = new EntityContext(); } protected void Application_EndRequest(Object sender, EventArgs e) { //-- Clean up the entitycontext var entityContext = HttpContext.Current.Items[Constants.ENTITYCONTEXT] as EntityContext; if (entityContext != null) entityContext.Dispose(); } </code></pre> <p>This is in my Repository base class, this get's the entityContext from HttpContext.</p> <pre><code>//-- EntityContext private EntityContext CurrentContext { get { return HttpContext.Current.Items[Constants.ENTITYCONTEXT] as EntityContext; } } </code></pre> <p>Here I get the Club I am going to use.</p> <pre><code>private Domain.Model.Club LoadClubByIdentifier(Guid identifier) { return this.CurrentContext.Clubs.SingleOrDefault(c =&gt; c.Identifier == identifier); } </code></pre> <p>Here is some of the code in my controllerclass that creates the user.</p> <pre><code>Domain.Model.User user = Mapper.Map&lt;Web.Content.Code.Models.User.CreatePlayer, Domain.Model.User&gt;(model); user.Identifier = new Guid().NewSequentialGuid(); //-- Get current club user.Clubs.Add(base.CurrentClub); //-- Get all teams and add them to user foreach (string teamIdentifier in model.Team) { Domain.Model.Team team = new Domain.Services.TeamServices().LoadByIdentifier( Domain.Helper.CryptationHelper.DecryptIdentifier(teamIdentifier)); user.Teams.Add(new TeamUser() { Team = team, User = user, UserTeamRelation = UserTeamRelationEnum.Player }); } //-- Create this user new Domain.Services.UserServices().CreatePlayer(user); </code></pre> <p>Here is the code in my userServices, doesn't do much yet...</p> <pre><code>public void CreatePlayer(Domain.Model.User user) { Password password = Password.GenerateRandomPassword(); user.Password = password; //-- Save the user to storage _userRepository.SaveUser(user); } </code></pre> <p>And here is the code in the userRepository that adds the new user.</p> <pre><code>//-- Methods private void SaveUser(Domain.Model.User user) { this.CurrentContext.Users.Add(user); this.CurrentContext.SaveChanges(); } </code></pre> <p>Best regards Magnus</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload