Linq2Sql:将实体重新附加到数据上下文,但不是原始数据上下文问题

发布于 2024-08-03 17:10:08 字数 972 浏览 5 评论 0原文

有人可以帮忙吗?我有一个 linq2sql 问题,并尝试通过数据上下文附加(更新)实体类,它抱怨它不是原始上下文,这是真的(参见下面的代码)...我认为打开数据上下文是最佳实践,并且不需要的时候关闭?

我基本上让我的应用程序调用一个服务层,该服务层又调用一个数据库上下文所在的存储库...您可以在这里看到我有 2 个数据上下文,每个方法中都定义了一个数据上下文...

基本上发生的事情是我的应用程序“获取”预订...然后应用程序更新实体类“预订”,然后将其重新发送到“更新”预订..任何人都可以帮忙..我完全被困在这里

基本上是我的代码

    public bool UpdateReservation(Reservation reservation)
    {
        bool success = false;
        try
        {
            ResDataContext db = new ResDataContext ();

            db.Reservations.Attach(reservation);
            db.SubmitChanges();
            success = true;

        }
        catch (Exception ex)
        {
            Console.WriteLine("");

        }


        return success;

    }

    public Reservation GetReservation(string reservationNumber)
    {
        ResDataContext db = new ResDataContext ();

        return db.Reservations.Where(r => r.ReservationNumber == reservationNumber).SingleOrDefault(); 
    }

can anyone help? I have an issue with linq2sql and trying to Attach (update) an entity class through a datacontext, it complains its not the original context, this is true (see code below) ... I thought it was best practices to open the datacontext and close it when not needed?

I basically have my app calling a service layer which in turn calls a repository where the db context is.... You can see here i have 2 data contexts, well a datacontext is defined in each method...

Basically what happens is my app "Gets" a reservation ... then the app updates the entity class "Reservation" and then resends it to "Updates" a Reservation.. Can anyone help.. I am completely stuck

here is basicallly my code

    public bool UpdateReservation(Reservation reservation)
    {
        bool success = false;
        try
        {
            ResDataContext db = new ResDataContext ();

            db.Reservations.Attach(reservation);
            db.SubmitChanges();
            success = true;

        }
        catch (Exception ex)
        {
            Console.WriteLine("");

        }


        return success;

    }

    public Reservation GetReservation(string reservationNumber)
    {
        ResDataContext db = new ResDataContext ();

        return db.Reservations.Where(r => r.ReservationNumber == reservationNumber).SingleOrDefault(); 
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗷兮 2024-08-10 17:10:08

问题是您没有重新附加到相同的数据上下文。

您需要在调用之间保持 ResDataContext 处于活动状态。

  • 在包含 ResDataContext 的类上创建一个属性。
  • 在构造函数中实例化 ResDataContext
  • 如果您的调用通过 WCF 进行,您将需要使用每个会话调用

这是 Eniity Framework(不是 linq to SQL),但问题类似:

实体框架: AttachAsModified 失败/混乱:)

The problem is that you are not reattaching to the same data context.

You need to keep the ResDataContext alive between the calls.

  • Create a property on the class which contains ResDataContext.
  • Instanciate ResDataContext in the constructor
  • If your calls are going over WCF you will need to use per session calls

This was Eniity Framework (not linq to SQL), but the problem was similar:

Entity Framework: AttachAsModified failure / confusion :)

烟沫凡尘 2024-08-10 17:10:08

您可以首先从 DataContext 收集正确的 Reservation 对象,然后更新并提交它。

像这样:

 public bool UpdateReservation(Reservation reservation)
{
    bool success = false;
    try
    {
        ResDataContext db = new ResDataContext ();

        Reservation res = db.Reservations.Where(r => r.ReservationNumber == reservation.ReservationNumber).Single();
        db.Reservations.InsertOnSubmit(res);
        db.SubmitChanges();
        success = true;

    }
    catch (Exception ex)
    {
        Console.WriteLine("");

    }


    return success;

}

You could first collect the correct Reservation object from the DataContext and then update and submit it.

Like this:

 public bool UpdateReservation(Reservation reservation)
{
    bool success = false;
    try
    {
        ResDataContext db = new ResDataContext ();

        Reservation res = db.Reservations.Where(r => r.ReservationNumber == reservation.ReservationNumber).Single();
        db.Reservations.InsertOnSubmit(res);
        db.SubmitChanges();
        success = true;

    }
    catch (Exception ex)
    {
        Console.WriteLine("");

    }


    return success;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文