I've started a sample project with code first and get NULL value from entity which has a foreign key constraint.
my model:
public class CustomerItem
{
[Key]
public int Id { get; set; }
public int Name{get;set;}
public virtual IEnumerable<OrderItem> Order { get; set; }
}
public class OrderItem
{
[Key]
public int Id { get; set; }
[ForeignKey("CustomerItem")]
public int CustomerId { get; set; }
public virtual CustomerItem CustomerItem { get; set; }
}
dataaccess linq query to get values:
public IEnumerable<CustomerItem> GetOrdersFromCustomerItem(int id)
{
return (from c in this.dax.Customer
where c.Id.CompareTo(id) == 0
select c).AsEnumerable().ToList();
}
this query return NULL orders so I think there's some mistake in this query. How can I wire up this entity with foreign key table?
Thanks
Updated my query to get all customers (and their orders) to this:
public IEnumerable<CustomerItem> GetAllCustomers()
{
return (from c in this.dax.Customer
select new
{
//Properties
OrderItem = c.OrderItem
}).AsEnumerable.ToList().select(s => new CustomerItem
{
//Properties
OrderItem = s.OrderItem, //here I got the exception I wrote in comment
}).ToList();
}