无法在WCF服务中加载相关实体 [英] Unable to load related entities in a WCF service

查看:48
本文介绍了无法在WCF服务中加载相关实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework 4.1。我需要创建一个WCF服务,该服务需要从现有数据库中提取。我正在使用wsHttp绑定。我已经将DbContext生成器与WCF支持一起使用。以下是已生成的两个类:

I'm using Entity framework 4.1. I need to create a WCF service which needs fetches from an existing database. I am using wsHttp binding. I have used the DbContext generator with WCF support. The following are two of the classes that have been generated:

[DataContract(IsReference = true)]
[KnownType(typeof(TestSummary))]
public partial class Test
{
    [DataMember]
    public int Id { get; private set; }

    [DataMember]
    public virtual TestSummary TestSummary { get; set; }
}

,以及

[DataContract(IsReference = true)]
[KnownType(typeof(Test))]
public partial class TestSummary
{
    [DataMember]
    public int Id { get; private set; }
    [DataMember]
    public string Summary { get; set; }

    [DataMember]
    public virtual Test Test { get; set; }
}

当我获取所有Test对象时,没有得到与TestSummary相关的信息对此。我所有其他实体也是如此。用于获取测试实体的代码:

When I fetch all the Test objects, I do not get the TestSummary related to it. The same happens with all my other entities. Code use to fetch the Test entity:

List<Test> tests = new List<Test>();
using (TestModelContainer context = new TestModelContainer())
{
    var query = (from t in context.Tests select t);

    foreach (var t in query)
    {
        Test test = (Test)t;
        tests.Add(test);
    }
}

但是,当我使用DbContext生成器生成类时在没有WCF支持的情况下,所有相关实体也会被加载(我已通过控制台应用程序对其进行了测试)。

However, when I generate the classes using the DbContext generator without WCF support, all the related entities are also loaded (I tested it with a console application).

推荐答案

您没有获取相关的 TestSummary 实体。您需要明确声明要通过在查询中添加 Include 语句来获取相关的TestSummaries。

in your query, you are not fetching related TestSummary entities. You need to declare explicitly that you want to fetch related TestSummaries with adding an Include statement to your query.

var query = (from t in context.Tests.Include("TestSummary") select t);

此外,也无需创建 List< Test>测试;您可能只是返回从查询中获取的内容;

Also, there is no need to create List<Test> tests; you may just return what you have fetched from the query;

using (TestModelContainer context = new TestModelContainer())
{
    var query = (from t in context.Tests select t);
    return query.ToList();
}

这篇关于无法在WCF服务中加载相关实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆