1

Been working on this C# Entity Framework update problem for a bit now. Maybe one of you can see what I'm missing.

The problem is within the userEntry portion of the code. I've traced through and made sure that userEntry is indeed populated with the information that I intend to update. When entities.SaveChanges(); is invoked, the record is not updated in the database.

            int userId;
            using (Entities entities = new Entities())
            {
                MSIFeedStoreData feedStoreEntry = entities.MSIFeedStoreDatas
                    .FirstOrDefault((d) => d.Alias == user.Alias);

                if (Object.ReferenceEquals(feedStoreEntry, null))
                {
                    throw new ArgumentException("The user's alias could not be located in the feed store. The user cannot be added at this time.");
                }

                int feedStorePersonnelIdValue;
                if (Int32.TryParse(feedStoreEntry.PersonellID, out feedStorePersonnelIdValue))
                {
                    user.EmployeeId = feedStorePersonnelIdValue;
                }
                else
                {
                    throw new ApplicationException("DATABASE BUG CHECK: An entry was found in the feed store for this user but the personnel ID could not be parsed.");
                }

                MSIUser userEntry = entities.MSIUsers
                    .FirstOrDefault((u) => u.EmployeeID == feedStorePersonnelIdValue);

                if (Object.ReferenceEquals(userEntry, null))
                {
                    userEntry = Mapper.Map<MSIUser>(user);
                    userEntry = entities.MSIUsers.Add(userEntry);
                }
                else
                {
                    Mapper.DynamicMap<User, MSIUser>(user, userEntry);
                    entities.MSIUsers.Attach(userEntry);
                }

                userId = userEntry.MSIUser_ID;
                entities.SaveChanges();
            }

            return userId;
        }
5
  • You not changing anything. What are your expected results? Commented Jul 25, 2013 at 21:43
  • Object.ReferenceEquals(feedStoreEntry, null) is much easier to read as feedStoreEntry == null and does the same thing. Commented Jul 25, 2013 at 21:50
  • The change is in here: else { Mapper.DynamicMap<User, MSIUser>(user, userEntry); entities.MSIUsers.Attach(userEntry); } There is a user entry passed in through an ajax PUT call. user is then Mapped to userEntry through Mapper.DynamicMap<User, MSIUser>(user, userEntry); I have verified through debugging that this is infact happening. Commented Jul 25, 2013 at 22:02
  • Don't call Attach after that, its already attached to the context when you loaded it. Unless your DynamicMap method returns a new object, in which case you need to attach as modified, not just plain attach. entities.Entry(userEntry).State = EntityState.Modified; after the Attach command. Commented Jul 25, 2013 at 22:04
  • Removing the attach worked. After the dozens of changes I made, the attach being one of which, the attach has been blocking whatever the original issue was. Thank you Charlie. Feel free to provide an answer and I will select it. Commented Jul 25, 2013 at 22:11

1 Answer 1

3

Remove the call to Attach, its already attached to the context.

Sign up to request clarification or add additional context in comments.

3 Comments

Edited to reflect comments
Remembered what the original issue was. MSIUser userEntry = entities.MSIUsers .FirstOrDefault((u) => u.EmployeeID == feedStorePersonnelIdValue); was originally written as MSIUser userEntry = entities.MSIUsers .AsNoTracking() .FirstOrDefault((u) => u.EmployeeID == feedStorePersonnelIdValue);
I made a similar mistake in confusing Attach with AddObject - no clue if that's relevant to this question because I didn't read the code but wanted to put this here anyway :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.