ADO.NET数据服务:非异步调用?

时间:2009-03-17 18:33:27

标签: asynchronous wcf-data-services

我有一个问题,我在ADO.NET数据服务中苦苦挣扎:

在为存储组装实体时,我需要从查找文件中获取相关值。例如,一个人的状态代码分配为“待定”,该状态代码位于名为StatusCodes的表中。

在Entity Framework中,我需要将person.StatusCode的值设置为等于StatusCode的实例。在Entity Framework或LINQ2Sql中我会这样:

      var person = Person.CreatePerson(stuff);

  var statCode = myContext.StatusCodeSet.Where(sc => sc.Description == "Pending").FirstOrDefault();

  person.StatusCode = statCode;

  // ...more code here...

  myContext.BeginSaveChanges(SaveChangesOptions.Batch,

                                new AsyncCallback(OnSaveAllComplete),

                                null);

statCode的查询在ADO.NET Data Services中不起作用,我收到运行时错误,说明该函数不受支持。我认为这是因为statCode查找不是异步调用。

然而,

      var person = Person.CreatePerson(stuff);
  var query = from stat in myContext.StatusCodeSet
              where stat.Description == "Pending"
              select stat;
  var dsQuery = (DataServiceQuery<StatusCode>)query;
  dsQuery.BeginExecute(
      result => tutorApplication.StatusCode = dsQuery.EndExecute(result).FirstOrDefault(), null);
  // ...more code here...
  myContext.BeginSaveChanges(SaveChangesOptions.Batch,
                            new AsyncCallback(OnSaveAllComplete),
                            null);
由于查询的异步性质,

不起作用,结果将在人员保存发生之前返回。

我接近这个吗?

由于

1 个答案:

答案 0 :(得分:0)

睡觉后,我想出了以下内容:

  var person = Person.CreatePerson(stuff);
  var appStatPending = new StatusCode()
  {
    StatusCodeId = (int)StatusCodes.Pending,
    Code = "Pending",
    Description = "Pending",
    EffectiveDate = DateTime.Now,
    EnteredBy = "",
    EnteredDate = DateTime.Now
  };

  myContext.AttachTo("StatusCodeSet", appStatPending);
  person.StatusCode = appStatPending;
  myContext.SetLink(tutorApplication, "StatusCode", appStatPending);


  // ...more code here...  
  myContext.BeginSaveChanges(SaveChangesOptions.Batch,
    new AsyncCallback(OnSaveAllComplete),
    null);

我可以创建状态代码的本地副本并将其链接到上下文中。重新启动appStatPending而不是执行StatusCode.CreateStatusCode()非常重要,因为这样做会在人员图持续存在时向数据库添加新的StatusCode。出于同样的原因,执行AttachTo(“StatusCodeSet”,appStatPending)很重要,因为执行myContext.AddToStatusCodeSet()也会在数据库中的StatusCodes表中添加一个新条目。

相关问题