Odata EntityFrameworkDataService:属性具有某种结构,但应为“导航”类型

Odata EntityFrameworkDataService:属性具有某种结构,但应为“导航”类型,odata,wcf-data-services,Odata,Wcf Data Services,我使用OData创建数据服务,因为使用了EF,所以我使用EntityFrameworkDataService创建这些服务。我在尝试linq名为order details的orderdetail实体时遇到以下错误 类型“WCFOData.BusinessEntities.EF.Order”上名为“OrderDetails”的属性具有“Structural”类型,但应为“Navigation”类型 框架和版本 实体框架6 OData版本3 下面是所用实体、上下文、EntityFrameworkDat

我使用OData创建数据服务,因为使用了EF,所以我使用EntityFrameworkDataService创建这些服务。我在尝试linq名为order details的orderdetail实体时遇到以下错误

类型“WCFOData.BusinessEntities.EF.Order”上名为“OrderDetails”的属性具有“Structural”类型,但应为“Navigation”类型

框架和版本 实体框架6 OData版本3 下面是所用实体、上下文、EntityFrameworkDataService和客户端的代码。注释中提到了客户端代码中的错误。谢谢你的帮助

   public partial class EFContext : System.Data.Entity.DbContext
   {
    public EFContext()
        : base("name=EFContext")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<Customer> Customers { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<OrderDetail> OrderDetails { get; set; }
    public DbSet<Product> Products { get; set; }
}

public partial class Customer
{
    public Customer()
    {
        this.Orders = new HashSet<Order>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string Phone { get; set; }

    public virtual ICollection<Order> Orders { get; set; }
}

public partial class Order
{
    public Order()
    {
       this.OrderDetails = new HashSet<OrderDetail>();
    }

    public int ID { get; set; }
    public int CustomerID { get; set; }
    public System.DateTime OrderDate { get; set; }

    public virtual Customer Customer { get; set; }
    public virtual ICollection<OrderDetail> OrderDetails { get; set; }
}

public class WcfODataService : System.Data.Services.Providers.EntityFrameworkDataService<EFContext>
{
    
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
    }
  }
//Client 
var context = new DataModelProxy(new Uri(@"http://localhost:9342/WcfODataService.svc/"));
         // Get the specific product.
        int productId = 25;
        try
        {
int customerId = 6959;

OrderDetail newItem = null;

var selectedcustomer = (from c in context.Customers
                       where c.ID == customerId
                       select c).Single();
var selectedOrder = (from c in context.Orders
                       where c.CustomerID == customerId   
                       select c).Single();                      ** //--Error on this line **
var selectedOrderDetail = (from c in context.OrderDetails
                     where c.OrderID == selectedOrder.ID
                     select c).Single();
var selectedProduct = (from c in context.Products
                           where c.Id == productId
                           select c).Single();
}
您应该使用DataServiceCollection,而不是使用ICollection命令。我刚刚遇到了完全相同的问题,我使用了IEnumerable反序列化到

幸运的是,我使用了一个服务引用为我创建了一些代码,经过检查,我发现生成的代码使用了DataServiceCollection。更改后,问题消失了,我的单元测试顺利通过