3

Greetings and thanks for reading my post.. :

I am updating an entry (Photo) within a using

using (context = new PhotoEntities())
{
    // ...

    context.Entry(photo).State = EntityState.Modified;
}

The problem is that when i save this entry using

success = context.SaveChanges() > 0;
if (success)
{
     FeedServices.CreatePhotoFeed(photo);
}

I still need to have the context since i'm inserting a new feed. So.. i have tried using a different context and i'm experiencing an error that says :

An error occurred while updating the entries. 
See the inner exception for details.",
"ExceptionType":"System.Data.Entity.Infrastructure.DbUpdateException",
"StackTrace":"   at System.Data.Entity.Internal.InternalContext.SaveChanges()\r\n  
 at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()\r\n   
at System.Data.Entity.DbContext.SaveChanges()\r\n   
at ServiceLibrary.Services.FeedServices.CreatePhotoFeed(Photo photo) 
in c:\\PhotoApp\\ServiceLibrary

Here is the code that fails:

public static void CreatePhotoFeed(Photo photo)
{
    using (var pcontext = new PhotoEntities())
    {
        // TODO : Guest access handling.,...
        var sourceUser = UserServices.GetLoggedInUser();

        Feed feed = new Feed();
        feed.UserId = sourceUser.Id;
        feed.PhotoId = photo.Id;
        feed.SourceUsername = sourceUser.Firstname + " " + sourceUser.Lastname;
        feed.TargetUsername = photo.User.Firstname + " " + photo.User.Lastname;
        feed.DateCreated = DateTime.UtcNow;
        feed.Feedtext = "lastet opp et nytt foto";
        feed.FeedType = FeedTypeEnum.FeedType.NewPhoto.ToString();

        pcontext.Feeds.Add(feed);

        // this throws the error
        pcontext.SaveChanges();
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Terje Nygård
  • 1,233
  • 6
  • 25
  • 48
  • What is the inner exception? To me the code appears fine. – Rufus L Sep 16 '14 at 21:40
  • Well... as soon as you said that.. I figured out that i could run a try catch block around the saveChanges context, and i figured out that running through the System.Data.Entity.Infrastructure.DbUpdateException ex i noticed that the problem was the table and how the ID field was built up :) – Terje Nygård Sep 16 '14 at 21:55
  • Thanks a bunch... that saved me quite much time actually :) How do i accept an answer for this question ? – Terje Nygård Sep 16 '14 at 21:55
  • IDENTITY(1,1) is... quite important when it comes to inserting.. just too bad that you dont get a logical error without running some complex (at least to me..) try catching... – Terje Nygård Sep 16 '14 at 21:57

1 Answers1

4

The answer to this question is just a reminder to myself and to everyone else having issues to understand why Entity Framework inserts, updates etc.. fails with no obvious chance to get a specific error out of it "out of the box".

So by doing stuff like this :

try
            {
                pcontext.SaveChanges();
            }
            catch (System.Data.Entity.Core.UpdateException e)
            {

            }

            catch (System.Data.Entity.Infrastructure.DbUpdateException ex) //DbContext
            {
                Console.WriteLine(ex.InnerException);
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException);
                throw;
            }

you can actually get the exact and very specific error you are looking for.

In my case.. i forgot to set Auto Increment on the identity, resulting in all records inserted was of ID 0 (zero).

Terje Nygård
  • 1,233
  • 6
  • 25
  • 48