views:

80

answers:

1

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?

+2  A: 

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.

Ladislav Mrnka
When I set Lazy Loading false, it works. Am I to assume that WCF cannot deal with IEnumerables with Lazy Loading? How does one typically deal with large data sets (hundreds of thousands of records), always filter to a subset?
Investor5555
Best approach is send only subset which is necessary. I think you can define operation with IEnumerable<Class>. On the client site it will be handled as array.
Ladislav Mrnka
The issue was lazy loading. I will need to read up more on how to handle large datasets with WCF.
Investor5555