Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for WCF to transport virtual properties of EF?

Is it possible for WCF to transport virtual properties?

I have these classes on my project:

[DataContract]
class Country
{
    [Key, DataMember] public int CountryId { get; set; }
    [DataMember] public string CountryName { get; set; }
}


[DataContract]
class Employee
{
    [Key, DataMember] public int EmployeeId { get; set; }
    [DataMember] public string EmployeeName { get; set; }
    [DataMember] public int ResidingInCountryId { get; set; }

    [ForeignKey("ResidingInCountryId"), DataMember]
    public virtual Country ResidenceCountry { get; set; }
}

I already have Include on my WCF:

db.Employees.Include("ResidenceCountry").Where(x => x.EmployeeId == 1);

But I got this error:

The underlying connection was closed: The connection was closed unexpectedly.

That error disappeared if I don't use virtual

If it interest anyone, I don't encounter any error on NHibernate when using virtual and Fetch combo

[UPDATE: 2011-05-12 3:05 PM]

I solved my problem by using the solution here: http://www.gavindraper.co.uk/category/entity-framework/

The only difference is I use this.Configuration.ProxyCreationEnabled = false; instead of this.ContextOptions.ProxyCreationEnabled = false;. I'm using Entity Framework 4.1

like image 812
Green Lantern Avatar asked May 12 '11 02:05

Green Lantern


1 Answers

Just making the solution given above by the asker himself (@GreenLantern) clearer (and perhaps the problem) to anyone that ends in this post...

Problem: serializing objects with virtual properties

Solution: set your DBContext object's Configuration.ProxyCreationEnabled to false.

var dbContext = new YourEntityModelObject();
dbContext.Configuration.ProxyCreationEnabled = false;
like image 164
Veverke Avatar answered Sep 28 '22 03:09

Veverke