Frameworks 实体框架:获取相关实体

Frameworks 实体框架:获取相关实体,frameworks,entity,entities,Frameworks,Entity,Entities,我用实体框架创建了一个WCF服务 我有两张桌子:剧院和地方。在剧院中,位置作为外键 我的方法: public theater[] GetTheaters() { using (Entities context = new Entities()) { return context.theater.ToArray(); } } 我必须从剧院类的“公共虚拟区域{get;set;}”中删除“virtual”关键字。否则,我会得到一个通信异常 但是当我这样做的

我用实体框架创建了一个WCF服务

我有两张桌子:剧院和地方。在剧院中,位置作为外键

我的方法:

public theater[] GetTheaters()
{

    using (Entities context = new Entities())
    {
        return context.theater.ToArray();

    }
}
我必须从剧院类的“公共虚拟区域{get;set;}”中删除“virtual”关键字。否则,我会得到一个通信异常

但是当我这样做的时候,我得到了我的剧院列表,但是地方是空的

我怎样才能知道地点

谢谢

我的模型类(我还有其他实体):

公共部分类局部性
{
公共场所()
{
this.theater=新HashSet();
}
public int idLocality{get;set;}
公共int npa{get;set;}
公共字符串locality1{get;set;}
公共ICollection剧院{get;set;}
}
公共半等剧场
{
公共剧场()
{
this.session=new HashSet();
}
公共int IDT剧院{get;set;}
公共字符串名称{get;set;}
公共字符串地址{get;set;}
public int idLocality{get;set;}
公共双纬度{get;set;}
公共双经度{get;set;}
公共int席位{get;set;}
公用字符串电话{get;set;}
公共字符串电子邮件{get;set;}
公共bool-threeD{get;set;}
公共位置{get;set;}
公共ICollection会话{get;set;}
}
以下是我得到的错误:

“类型“locality”的对象图包含循环,如果禁用引用跟踪,则无法序列化

编辑:

我找到的解决方案是:

在我所在地的班级里,我有一批剧院

我不得不在setter中添加“private”,如下所示:

“公共ICollection剧院{get;private set;}”

这是可行的,但我仍然有一个问题,我不能再从本地实体访问剧院了。(不再双向)

您可以使用。使用“快速加载”时,您可以使用扩展方法:

return context.Theater.Include(t => t.Locality).ToArray();

如果要强制加载相关实体,可以使用。默认情况下,相关实体以延迟方式加载

你的例子是:

public theater[] GetTheaters()
{

    using (Entities context = new Entities())
    {
        return context.theater.Include(t=>t.Locality).ToArray();

    }
}

您缺少创建关系的正确注释。请参阅下面的代码。(如果使用FluentAPI,也可以自己创建关系)

查找
[Key]
[ForeignKey]
注释,以及
虚拟
关键字

public partial class locality
    {
        public locality()
        {
            //this.theater = new HashSet<theater>();
        }

        [Key]
        public int idLocality { get; set; }

        public int npa { get; set; }
        public string locality1 { get; set; }

        public virtual ICollection<theater> theaters { get; set; }
    }


    public partial class theater
    {
        public theater()
        {
            //this.session = new HashSet<session>();
        }

        [Key]
        public int idTheater { get; set; }

        public string name { get; set; }
        public string address { get; set; }
        public int idLocality { get; set; }
        public double latitude { get; set; }
        public double longitude { get; set; }
        public int seats { get; set; }
        public string phone { get; set; }
        public string email { get; set; }
        public bool threeD { get; set; }

        [ForeignKey("idLocality")]
        public virtual locality locality { get; set; }
        //public ICollection<session> session { get; set; }
    }
公共部分类局部性
{
公共场所()
{
//this.theater=新HashSet();
}
[关键]
public int idLocality{get;set;}
公共int npa{get;set;}
公共字符串locality1{get;set;}
公共虚拟ICollection剧院{get;set;}
}
公共半等剧场
{
公共剧场()
{
//this.session=new HashSet();
}
[关键]
公共int IDT剧院{get;set;}
公共字符串名称{get;set;}
公共字符串地址{get;set;}
public int idLocality{get;set;}
公共双纬度{get;set;}
公共双经度{get;set;}
公共int席位{get;set;}
公用字符串电话{get;set;}
公共字符串电子邮件{get;set;}
公共bool-threeD{get;set;}
[外键(“空闲”)]
公共虚拟位置{get;set;}
//公共ICollection会话{get;set;}
}

为您的模型类发布更多代码。那部分你可能有点不对劲谢谢。我已经试过了,但是我得到了一个通讯异常。。。我是否需要在edmx模型的属性中将“LazyLoading Enabled”设置为False?在我的剧院类中保留“public virtual locality{get;set;}”中的“virtual”关键字?是的,在处理需要序列化的类型时,仍然应该禁用延迟加载。导航属性上的virtual关键字是延迟加载的要求之一,因此删除它会禁用它,但您应该显式地将其关闭(或完全关闭代理创建)。下面是我在测试客户端上的错误(抱歉,法语部分…):这看起来像是一个单独的问题,服务是否在该端点上运行和公开?抱歉,我是一个初学者,并没有完全理解你的问题,但如果我尝试只加载剧院而不加载区域,它会起作用。所以这应该不是服务的问题…谢谢,我找到了另一个解决方案,但是如果我仍然有问题,我会看看这个。你可以发布你的其他解决方案作为答案,并选择它作为解决方案。我是新来的,所以我在询问后8小时内无法添加答案:)
public partial class locality
    {
        public locality()
        {
            //this.theater = new HashSet<theater>();
        }

        [Key]
        public int idLocality { get; set; }

        public int npa { get; set; }
        public string locality1 { get; set; }

        public virtual ICollection<theater> theaters { get; set; }
    }


    public partial class theater
    {
        public theater()
        {
            //this.session = new HashSet<session>();
        }

        [Key]
        public int idTheater { get; set; }

        public string name { get; set; }
        public string address { get; set; }
        public int idLocality { get; set; }
        public double latitude { get; set; }
        public double longitude { get; set; }
        public int seats { get; set; }
        public string phone { get; set; }
        public string email { get; set; }
        public bool threeD { get; set; }

        [ForeignKey("idLocality")]
        public virtual locality locality { get; set; }
        //public ICollection<session> session { get; set; }
    }