开发者

Entity Framework 4.0, PoCo & Navigation Properties for Lookup Tables

开发者 https://www.devze.com 2023-02-11 13:53 出处:网络
I have the following Models DeltaDirectionType, int Id string Name Delta, int Id string Name DeltaDirectionType DeltaDirectionType

I have the following Models

DeltaDirectionType,
int Id
string Name


Delta,
int Id 
string Name 
DeltaDirectionType DeltaDirectionType
Double Value

Trade
int Id
DateTime BusinessDate
IList<Delta> deltas

So DeltaDirectionType is a lookup table, Trade holds a collection of Deltas

In the database it is implemented as follows

DeltaDirectionTypes
Id int
Name varchar(max)

Deltas
Id int
Name varchar(max)
DeltaDirectionType_Id int
Trade_Id int
Value float

Trades
Id int
BusinessDate DateTime
Delta_Id int

When I generate the model from code for the Edmx file, and (un check the foreign keys) as my model does not have properties for these. I am having problem with the navigation properties. Something like this i开发者_C百科n nHibernate would be a simple one to many mapping for the DeltaDirectionType and Delta and a many to many for Delta and Trades however, how do I firstly get it to recognise that DeltaDirectionType is a lookup and secondly get the Icollection to work for me.

I am struggling with this, Entity Framework does not make it easy for you. I have tried the usual, delete the navigation property that EF puts in for you on one side, but then you get some mapping fragments errors, properties not mapped etc.

Please help or point in the right direction.

Lookup tables are real life problems, not sure why it is so hard withing EF to implement.

Any help much appreciated

Thanks


You must either create foreign keys or navigation properties in the model to navigate the relationship.

Navigation properties can be defined as one-way, ie from the deltas table to the look-up table. A one-way navigation like this would add the appropriate property to the delta's object but not to the looup table


What you actually mean by lookup? Do you except that Delta entity will have DeltaDirectionType_Name directly mapped?

In EF you will get navigation property to DeltaDirectionType and you can access the name through this navigation property. If you don't like it you can add new property to partial class of your generated POCO and provide the Name directly in Delta entity like:

public string DeltaDirectionTypeName
{
  get
  {
    return DeltaDirectionType != null? DeltaDirectionType.Name : String.Empty;
  }
}

The only problem is that you can't use this property in Linq-To-Entities queries. In queries you always have to use navigation properties.

0

精彩评论

暂无评论...
验证码 换一张
取 消