views:

1073

answers:

1

I am just getting started with Microsoft's Entity Framework, using it for an MVC project since MS seems to be really pushing it, and I'm running into some issues. In my database there are multiple lookup tables tied to a single table via foreign keys. Within the entity framework I am trying to combine these into one so that I have a simplified single view for this data in my model. However, this doesn't seem possible from the designer view. Is there something obvious I'm missing? Is there a way that I can edit the edmx file manually to produce this sort of model?

+3  A: 

At the moment, Foreign keys and lookup tables in Entity Framework are a PAIN.

EF with LINQ makes getting your data super-easy, and on the surface it looks easy to update, but with lookup tables things get difficult (for now... read on...)

I'm not sure how you would "combine" your lookup tables into a single table. If each table contains a different type of "lookup entity" then IMHO they should be represented separately in your EDM. I'm guessing you're having headaches updating a record's foreign keys to the lookup tables. That's because it is a headache.

Changing foreign key values:

MyDBEntities _db = new MyDBEntities();
//get a Person
MyDBEntities.Person person = (from p in _db.Persons
                      where p.Id = 1
                      select p).First();
// This sets the foreign key value in the Person table on the PersonType field
person.PersonTypeReference = new EntityKey("MyDBEntities.PersonType", "PersonTypeId", 3)

The next release version of the Entity Framework will have a new concept called "FK Associations." This will bring back the sanity of setting the foreign key value directly rather than having to create and set an EntityKey.

HTH.

Dave Swersky
Correction: There is already a 'Release version' it ships in .NET 3.5 SP1. There will be another version that will ship in .NET 4.0.Sorry I'm picky around this stuff, it's only because it perpetuates the mistaken notion that the EF hasn't shipped yet ;)
Alex James
Ah ok I see. This pretty much answers my question. For clarity's sake what I meant by combining was essentially creating a view in the model (I'm pretty new to ORM so some of my terminology could be off) based off of all of these lookup tables. The database I'm working with is structured in a highly normalized fashion so there are lookup tables for state, country, and everything else.
Graham
PersonTypeReference is presumably an EntityReference? Is EntityKey really assignable to EntityReference?
Christopher
Should that be `person.PersonTypeReference.EntityKey = ...` ?
codeulike