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();
}
}