2013-06-19 222 views
0

我使用实体框架创建了一个WCF服务。实体框架:获取相关实体

我有2个表:剧院和地点。地点作为剧院中的外键。

我的方法:

public theater[] GetTheaters() 
{ 

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

    } 
} 

我不得不删除从“虚拟”关键字“公共虚拟局部性地区{获取;集;}”在我的戏剧课。否则,我得到一个CommunicationException。

但是,当我这样做,我让我的剧场名单,但局部性空...

我怎样才能得到当地?

感谢

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

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

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

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


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

     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; } 

     public locality locality { get; set; } 
     public ICollection<session> session { get; set; } 
    } 

以下是错误,我得到:

“对象图表类型‘地方’包含周期和如果参考跟踪被禁用无法序列

编辑:

我找到的解决方案:

在我所在的班级里,我收藏了剧院。

我不得不添加 “私人像这样的setter:

” 公共ICollection的剧场{获得;私人设置; }”

所以它的工作原理,但我仍然有一个问题,我无法访问从当地实体剧院了。(没有更多的双向)

+0

为您的模型类发布更多代码。您可能在该部分有问题 – Omar

回答

0

可以使用eager loading or explicit loading随着预先加载您使用Include扩展方法:

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

谢谢。我已经试过这个,但我得到一个CommunicationException ...我需要在我的EDMX模型的属性中设置“LazyLoading Enabled”为False吗?并在剧场课程中将“虚拟”关键字从“public virtual locality locality {get; set;}”保留下来? – user2157493

+0

是的,在处理需要序列化的类型时,仍然应该禁用延迟加载。导航属性上的虚拟关键字是延迟加载的要求之一,因此删除它将禁用它,但您应该明确地将其关闭(或完全创建代理)。 – devdigital

+0

这是我的错误与测试客户端(对不起,部分法语...):https://dl.dropboxusercontent.com/u/9197067/commerror.png – user2157493

0

如果要强制相关实体加载,你可以使用Include method这样做默认情况下,相关的实体延迟加载

你的榜样将b。 E:

​​
0

你错过了正确的注释来创建关系。请参阅下面的代码。 (或者使用FluentAPI自己创建关系)

查找[Key][ForeignKey]注释以及virtual关键字。

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; } 
    } 
+0

谢谢,我找到了另一个解决方案,但如果我仍然有问题,我会查看这个 – user2157493

+0

您可以发布您的其他解决方案作为答案,并选择它作为解决方案 – Omar

+0

我是新来的所以我不能添加回答8小时后问:) – user2157493