私はEFでWCFサービスを書いています。私の問題は、顧客エンティティの子を返そうとしたときに発生します。サンプルコードの下:
[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; }
}
私が得るエラー:
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.
私が呼び出すサービス機能:
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!");
}
私はそれをデバッグしようとしましたが、この関数は Customer とその子 BankAccounts を返します。遅延読み込みも無効にしました。この行をコメントアウトすると、
public virtual ICollection<BankAccount> BankAccounts { get; set; }
顧客クラスを作成すると、BankAccount を取得できないことを除いてすべてが機能し、Customer のみが返されます。私はWCFが初めてなので、助けてください。ありがとう。
だから私は問題を解決する方法を見つけました。BankAccount からの顧客参照を IgnoreDataMember としてマークする必要がありました
[IgnoreDataMember]
public virtual Customer Customer { get; set; }
MyDbContext コンストラクターで ProxyCreation を無効にします。
this.Configuration.ProxyCreationEnabled = false;