EntityFrameworkDataService:属性具有类型结构,但预计它将是一种“导航”

时间:2014-12-24 08:27:00

标签: odata wcf-data-services

我正在使用OData创建数据服务,因为使用了EF我正在使用EntityFrameworkDataService来创建这些服务。我在orderdetail实体上尝试linq时遇到以下错误,称为订单详细信息。

“类型为'WCFOData.BusinessEntities.EF.Order'的名称为'OrderDetails'的属性具有类型'结构',但预计会有类似的'导航'

框架&版本

  • 实体框架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();
}

1 个答案:

答案 0 :(得分:0)

您应该使用ICollection<OrderDetails>而不是DataServiceCollection<OrderDetails>订单。我刚遇到完全相同的问题,我使用IEnumerable<T>反序列化。

幸运的是,我使用了服务参考为我创建了一些代码,经过检查,我发现生成的代码使用了DataServiceCollection<T>。更改后问题消失了,我的单元测试没有失败。