SL:为什么我的DomainService将结果查询返回为null?

时间:2012-08-19 19:35:13

标签: c# silverlight wcf-ria-services domainservices

我现在正在使用Silverlight和RIA Services。

在我的项目中,我有一个DomainService和一个AuthenticationService。

当我进行身份验证时,我意识到如果我调试ObjectContext,我可以看到我数据库中的所有记录。

但是当我使用我的DomainService时,我正试图从默认查询中获取对象,对于I.E. GetStudents但查询总是返回0个元素。

但是从它开始,我想做一个Insert,它有效

            // Has finished
            var jsonObjects = JsonConvert.SerializeObject(Test, Formatting.Indented);

            var context = new DatabaseDomainContext();
            // it works!! add the object
            //Student newStudent = new Student();
            //newStudent.Id = "OPA-3DKCL2";
            //newStudent.FirstName = "Oscar";
            //newStudent.LastName = "Fimbres";

            //context.Students.Add(newStudent);
            //context.SubmitChanges();

            // all the time returns 0 elements
            var students2 = context.Load(context.GetStudentsQuery()).Entities;

            // the same
            var students = context.GetStudentsQuery();
            AnsweredTest answerTest = new AnsweredTest();
            answerTest.JsonTest = jsonObjects;
            answerTest.Date = DateTime.Now;
            //answerTest.Student = context.Students.SingleOrDefault(x => x.Id == "OPA-3DKCLS");

enter image description here

如果我遗漏了重要数据,请告知我们。

1 个答案:

答案 0 :(得分:2)

加载操作是异步的,您需要订阅Completed事件并在那里获得结果:

var loadOperation = context.Load(context.GetStudentsQuery());
operation.Completed += OnStudentsLoaded;

private void OnStudentsLoaded(object sender, EventArgs e)
{
    var operation = sender as LoadOperation<Student>;
    if (operation == null)
    {
        throw new ArgumentException("sender is not LoadOpearation<Student>");
    }
    IEnumerable<Student> students = operation.Entities;

    //.....
}