使用一个上下文更新一个实体,并使用另一个上下文插入一个新实体? [英] Updating one entity with one context, and inserting a new with another context?

查看:52
本文介绍了使用一个上下文更新一个实体,并使用另一个上下文插入一个新实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,并感谢您阅读我的文章。:



我正在使用


$ b $更新条目(照片) b

 使用(context = new PhotoEntities())
{
// ...

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

问题是当我使用

$ b保存此条目时
$ b

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

由于我要插入新的供稿,因此仍然需要上下文。所以..我尝试使用不同的上下文,遇到一个错误:

 更新条目。 
有关详细信息,请参见内部异常。,
ExceptionType: System.Data.Entity.Infrastructure.DbUpdateException,
StackTrace:在System.Data.Entity.Internal .InternalContext.SaveChanges()在System.Data.Entity.Internal.LazyInternalContext.SaveChanges()在$ System.Data.Entity.DbContext.SaveChanges中的
()在ServiceLibrary.Services.FeedServices.CreatePhotoFeed($ photo)中的
,位于c:\\PhotoApp\\ServiceLibrary

这是失败的代码:

 公共静态无效CreatePhotoFeed(照片)
{
使用(var pcontext = new PhotoEntities())
{
// TODO:访客访问处理。,...
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);

//这会引发错误
pcontext.SaveChanges();
}
}


解决方案

这个问题的答案仅是对我自己和其他所有人的提醒,他们需要了解为什么Entity Framework进行插入,更新等操作。失败并没有明显的机会立即可用解决特定错误。



通过执行以下操作:

 尝试
{
pcontext.SaveChanges();
}
捕获(System.Data.Entity.Core.UpdateException e)
{

}

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

catch(例外)
{
Console.WriteLine(ex.InnerException);
投掷;
}

您实际上可以得到所要查找的准确且非常具体的错误。 / p>

在我的情况下。.我忘记在身份上设置自动递增,导致插入的所有记录的ID为0(零)。


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

解决方案

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).

这篇关于使用一个上下文更新一个实体,并使用另一个上下文插入一个新实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆