views:

25

answers:

0

I have recently been trying to learn more about the Entity Framework technologies from Microsoft. I have found a situation which I am not sure if it is possible (or correct) to solve using EF.

I have lots of entities that can be referred to by lots of different names. For example, a persons occupation maybe described as "Coder", "Programmer" or "Computer Programmer" which I want mapped to the same instance of an object.

I will have lots of different entities which will require the same functionality.

Is it possible to model this situation using EF, without having to manually create the additional "AlternativeName" table(s) myself? This could either use one table, or multiple tables.

For example, the columns in a one table setup may look like this:

Id, EntityID, EntityType, AlternativeName

Or alternatively, for each entity I create it could automatically create the table itself e.g. I have a class Foo that maps to a table "Foo", could I get EF to automatically create a table on the fly? e.g

Foo.TableName + "AlternativeNames"

Now I am not confident with the clarity of my question, so hopefully an example of how I want to use this would help:

Foo myFoo = NameManager.RetrieveByAltName<Foo>("Bar");
Foo mySecondFoo = NameManager.RetrieveByAltName<Foo>("FooFoo");

DifferentFoo diffFoo = NameManager.RetrieveByAltName<DifferentFoo>("Foo");

if (diffFoo == null)
{
    diffFoo = new DifferentFoo();
    diffFoo.AddAlternativeName("Foo"); //This would add into the alternative name table
}

How do I actually implement this in EF? Or would you advise not to?