I'm writing a WCF service with EF. My problem occurs when trying to return the children of customer entity. Below the sample code:
[DataContract]
public class Customer
{
[Key]
[DataMember]
public int CustomerID { get; set; }
[DataMember]
public string FirstName { get; set; }
// Customer has a collection of BankAccounts
[DataMember]
public virtual ICollection<BankAccount> BankAccounts { get; set; }
}
[DataContract(IsReference = true)]
public class BankAccount
{
[Key]
[DataMember]
public int BankAccountID { get; set; }
[DataMember]
public int Number { get; set; }
// virtual property to access Customer
//[ForeignKey("CustomerID")]
[Required(ErrorMessage = "Please select Customer!")]
[DataMember]
public int CustomerID { get; set; }
[DataMember]
public virtual Customer Customer { get; set; }
}
The error i get:
An error occurred while receiving the HTTP response to http://localhost:8732/Design_Time_Addresses/MyServiceLibrary/MyService/. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.
The service function that i call:
public Customer GetCustomer(int customerId)
{
var customer = from c in dc.Customers
where c.CustomerID == customerId
select c;
if (customer != null)
return customer.FirstOrDefault();
else
throw new Exception("Invalid ID!");
}
I tried to debug it, and this function returns the Customer and it's children BankAccounts, i also disabled lazy loading. I found out that if i comment out this line
public virtual ICollection<BankAccount> BankAccounts { get; set; }
form customer class, everything works except i can't get the BankAccount, it only returns the Customer. I'm new to WCF so please help me out. Thanx.
So i found how to fix the problem. Just had to mark Customer reference from BankAccount as IgnoreDataMember
[IgnoreDataMember]
public virtual Customer Customer { get; set; }
and diable ProxyCreation in MyDbContext constructor.
this.Configuration.ProxyCreationEnabled = false;