views:

187

answers:

2

Hi,

I'm currently working with a large database (approx. 500 tables) all without any foreign keys define.

My question is there an easy way to set up the relationships within entity framework (version 1 or 2) without doing it all manually?

Also some of the tables have a complex relationship type. For example a customer has a parentID but this can either link to another customer in the same table (customerID) or link to an account in an account table (accountID). Is this kind of relationship possible in entity framework?

If this is not possible or if anyone has any opinions on an alternative solution to Enitity Framework I'm more than open to ideas. Will nHibernate or active record be a better solution? Or will it be easier creating my own business object and data access?

Cheers

Simon

+2  A: 

If you don't have any Foreign Keys defined, then there's no way for the Entity Framework to infer relationships. You'll have to define them manually.

As for your second question...no. That kind of relationship is not possible (it's also a poor design choice).

It sounds to me like, unless you want to refactor your database and implement a design that has Foreign Key relationships, you're going to have to hand roll your own Business Objects and Data Access Layer.

Justin Niessner
I would say that the weird relationship is possible for an expert to map, but not worth the effort vs. fixing it.
Craig Stuntz
@Craig It'd be an interesting experiment. I don't know how EF would enforce a field having a parent that could be one type or another (of course, you could write the code in the partial class yourself...but that defeats the purpose).
Justin Niessner
The existing DB relationship is effectively one-way -- you can semi-reliably navigate from a parent customer to a child customer or from an account to a child customer. So you can make the EF relationship one way as well (possible in EDMX in EF 1 but not with the GUI; in EF 4 there is GUI support for one-way relationships).
Craig Stuntz
A: 

The general idea is to avoid EF at all, at least until Microsoft fix some of its problems.

My question is there an easy way to set up the relationships within entity framework (version 1 or 2) without doing it all manually?

No there isn't.

Also some of the tables have a complex relationship type. For example a customer has a parentID but this can either link to another customer in the same table (customerID) or link to an account in an account table (accountID). Is this kind of relationship possible in entity framework?

No, it isn't.

Or will it be easier creating my own business object and data access?

I think it's the easiest way, unfotunately. I worked a few months ago trying to use nhibernate in a legacy database like the one you have, and it was very frustating... (in the end we refactored the database and migrated the data)..

Best of luck.

Pedro
Obivously some EF fan would downvote me.. In the following link there are a couple of good reasons why to avoid EF. http://efvote.wufoo.com/forms/ado-net-entity-framework-vote-of-no-confidence/
Pedro