ADO.NET数据服务:非非同步电话?
-
19-08-2019 - |
题
我有我与ADO.NET数据服务挣扎了问题:
当组装一个实体储存我需要从查找文件中的相关值。例如一个人有分配“待定”,这是在表中称为StatusCodes的状态代码。
在实体框架,我需要设置person.StatusCode的等于所述的StatusCode的一个实例的值。在实体框架或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数据服务工作,我得到的是不支持运行时错误说法的功能。我想这是因为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);
不工作,要么由于查询的异步性质,结果将不保存情况的人之前回来。
我是否正确地处理这个?
由于
解决方案
睡在此之后我想出了以下内容:
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添加到数据库中,当人坚持图形做StatusCode.CreateStatusCode()。出于同样的原因,做AttachTo(“StatusCodeSet”,appStatPending),因为这样做myContext.AddToStatusCodeSet()也将加入数据库中的新条目StatusCodes表是很重要的。
不隶属于 StackOverflow