I am having problems setting up an association in entity framework between two tables where the association does not contain all the primary key values.
e.g. I have two tables (this is a contrived example, but it is sufficient to represent my real database which I cannot change)
------Items------ ---Orders----
----------------- -------------
* ItemId - * OrderId -
* EffectiveDate - - OrderDate -
- Name - - ItemId -
----------------- ------------- * denotes primary key field
Ideally I'd like a property on Orders that indicates the Item that was effective at OrderDate, but I could live with an association on Order to a collection of Items and then create a readonly property on Order that selected the correct Item.
EDIT: The database and model will be read-only so read-only solutions are OK.
Is this possible in entity framework? (or even LINQ to SQL?)
I believe that it is possible using NHibernate (can anyone confirm?) but I keep hitting brick walls with entity framework. So far the only solution I have managed is to create a property in the partial class for Order that uses a 'hack' to access the ObjectContext from order and query the context.Items collection directly as such
private IEnumerable<Item> Items
{
get
{
var ctx = this.GetContext();
return from i in ctx.Items where i.ItemId == this.ItemId select i;
}
}
public Item Item
{
get
{
return (from i in Items
where i.EffectiveDate <= this.OrderDate
orderby i.EffectiveDate ascending
select i).First();
}
}
Is there a better solution?