我在这个问题上打破了我的头。我在网上找到了关于它的东西,但没有一个明确的答案。我的问题:带EF代码优先的WebApi在生成父子关系时产生错误
我有类在MVC3 Web应用程序的模型部分: 父类和ChildClass 在父类有类型列表的属性Children
我使用EF代码首先,整齐地产生父表和我的数据库中的子表。
现在我需要一个REST服务,它返回一个List或一个ParentClass。
当我从ParentClass中删除属性Children时没有问题。但是对于这个儿童,我一直在听到一个错误。
错误:"The type System.Data.Entity.DynamicProxies.ParentClass_A0EBE0D1022D01EB84B81873D49DEECC60879FC4152BB115215C3EC16FB8003A was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."}
一些代码:
类:
public class ParentClass
{
public int ID { get; set; }
public string Name {get;set;}
public virtual List<ChildrenClass> Children { get; set; }
}
public class ChildrenClass
{
public int ID { get; set; }
public string MyProperty { get; set; }
}
服务:
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyService
{
static MyContext db;
public MyService() { db = new MyContext(); }
[WebGet(UriTemplate = "")]
public List<ParentClass> GetParents()
{
var result = db.Parents.ToList();
return result;
}
callinh这项服务的时候我不会得到结果。我究竟做错了什么?
正是我在找的。你知道禁用这个的缺点吗? – hooked82
代理有很多好处,记录更新等。忘记包含语句没有错误。我还没有序列化proxi实体的解决方案 –