Reputation: 61
I am creating data services using OData since EF is used I am using EntityFrameworkDataService to create these services. I am getting the below error when trying to linq on orderdetail entity called order details.
"A property with name 'OrderDetails' on type 'WCFOData.BusinessEntities.EF.Order' has kind 'Structural', but it is expected to be of kind 'Navigation"
Below is the code for entities used, context ,EntityFrameworkDataService and client. Error in Client code is mentioned in comments. Appreciate any help.
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();
}
Upvotes: 1
Views: 1202
Reputation: 564
Instead of using an ICollection<OrderDetails>
Orders you should use a DataServiceCollection<OrderDetails>
. I just ran into the exact same problem where I used an IEnumerable<T>
to deserialize into.
Fortunately, I had used a Service Reference to create some code for me and upon inspection I discovered that the generated code used a DataServiceCollection<T>
. After changing the problem went away and my unit tests passed without failure.
Upvotes: 1