如何更新从另一个类传递的实体对象?

时间:2013-02-25 00:50:44

标签: c# linq entity-framework

这是我的查询,我将返回IEnumerable<CreditCardTransaction>进行迭代。

  public partial class CreditCardTransaction
  {

    public static IEnumerable<CreditCardTransaction> GetUnprocessedTransactions()
    {
      try
      {
        using (var context = new SuburbanEntities())
        {
          return from trans in context.CreditCardTransactions
                 where trans.IsPublished == false
                 select trans;
        }
      }
      catch (Exception ex)
      {
        Logging.Log("An error occurred.", "GetUnprocessedTransactions",Apps.ServicesConfig, ex);
        return null;
      }
    }

  }

这是我在处理完这些交易后修改这些交易的地方:

public void ProcessFile()
{
  try
  {
    _client = new TruckServiceClient();
    _globalSetting = new GlobalSetting();

    var unprocesstransactions = CreditCardTransaction.GetUnprocessedTransactions();
    foreach (var creditCardTransaction in unprocesstransactions)
    {
      creditCardTransaction.IsPublished = ProcessTransaction(creditCardTransaction);
    }     
  }
  catch (Exception ex)
  {
    Logging.Log("An error occurred.", "ProcessCreditCardTransactions.ProcessFile", Apps.RemoteServices, ex);
  }
}

我在这里修改交易:

creditCardTransaction.IsPublished = ProcessTransaction(creditCardTransaction);

但是一旦我保存了它们,我可以直接更新实体,还是需要创建另一种方法来重新传递这些信息?

1 个答案:

答案 0 :(得分:1)

您无法访问上下文的问题。这里有一些例子如何做: http://cgeers.com/2009/02/21/entity-framework-objectcontext/

如果您正在开发Asp.Net应用程序,那么您将在本文中找到一些缺点: http://dotnetslackers.com/articles/ado_net/Managing-Entity-Framework-ObjectContext-lifespan-and-scope-in-n-layered-ASP-NET-applications.aspx#managing-objectcontext-instantiation