WCF与实体框架错误 [英] WCF with Entity Framework Error

查看:193
本文介绍了WCF与实体框架错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:ObjectContext实例已处理,不能再用于需要连接的操作。



我试图创建一个WCF服务与Entity框架(VS 2010,.NET 4)。当我运行它,我得到以上错误。



我读了一些关于编辑T4模板的东西,但它似乎已经有

  [DataContractAttribute(IsReference = true)] 
public partial class Person:EntityObject

  [DataMemberAttribute()] 
public global: :System.Int32 ID
{
get
{
return _ID;
}

我不知道

之间有什么区别

  [DataMemberAttribute()]和[DataMember] 

  [DataContractAttribute(IsReference = true)]和[DataContract] 



  public Person GetPersonByID (int id)
{
using(var ctx = new MyEntities())
{
return(from p in ctx.Person
其中p.ID == id
select p).FirstOrDefault();
}
}

WCF和EF如何正常工作? / p>

解决方案

您的Person类中是否有导航属性?你禁用延迟加载吗?否则,它可能会尝试在序列化过程中加载导航属性的内容,并且由于关闭的上下文而失败。



对您的其他问题:



[DataMemberAttribute()]和[DataMember]是相同的。它只是较短的名称。



[DataContractAttribute(IsReference = true)]和[DataContract]不相同。 IsRefrence允许跟踪导航属性中的循环引用。没有此参数,循环引用会导致永不结束递归。


Error: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

I am trying to create a WCF service with Entity Framework (VS 2010, .NET 4). When I run it, I get the above error.

I read something about editing the T4 template, but it appears that it already has

[DataContractAttribute(IsReference=true)]
public partial class Person : EntityObject

and

   [DataMemberAttribute()]
        public global::System.Int32 ID
        {
            get
            {
                return _ID;
            }

I am not sure what the difference is between

[DataMemberAttribute()] and [DataMember] 

or

[DataContractAttribute(IsReference=true)] and [DataContract] 

either.

 public Person GetPersonByID(int id)
        {
            using (var ctx = new MyEntities())
            {
                return (from p in ctx.Person
                        where  p.ID == id
                        select p).FirstOrDefault();
            }
        }

How does WCF and EF work together, properly?

解决方案

Do you have navigation properties in your Person class? Did you disable lazy loading? Otherwise it will probably try to load content for navigation properties during serialization and it fails because of closed context.

To your other questions:

[DataMemberAttribute()] and [DataMember] are same. It is just shorter name.

[DataContractAttribute(IsReference=true)] and [DataContract] are not same. IsRefrence allows tracking circular references in navigation properties. Without this parameter circular reference causes never ending recursion.

这篇关于WCF与实体框架错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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