Friday, 28 October 2011

Entity Framework Split Tables Model First

Instead of explaining the reasons why you might want to use the split table method, you can read this brillant series (written in a Code First perspective, hence the reason for this post):

Associations in EF 4.1 Code First

And to get an example to work from, see this:

How to work with table relationships and entity inheritances in ADO.NET Entity Data Model

How To Do Split Table Model First Style

1. Create your main entity, with a Primary Key eg PersonID - this is the one which will have a corresponding table in the DB eg Person
2. Create your child entity, with a Primary Key - this is the one which will map to certain fields in the DB table but will be a seperate Code entity eg PersonDetails
> Make sure that you name the Primary Key of this entity to be that of the Entity in step 1, eg PersonID
3. Create a 1 to 1 association between the two
4. Right click the association: Properties > Referential Constraint - the Principle will be the entity you created in Step 1 eg Person...note that the Priniciple Key and the Dependent Key should be the same name as defined in Steps 1 and 2
5. Open the Model Browser (right click on some white space in the EDMX Designer > "Model Browser"
6. Right click the Dependent entity (eg PersonDetails) and select 'Table Mapping'
7. In the Mapping Details dialog, select the Physical Principle Table eg Person
> If you don't seen Person here, then renerate the SSDL (store schema) and MSL (mappings) by right clicking on the EDMX white space and choose: "Generate Database from Model"
8. Now map the Table Columns from the Priniciple table to the Scalar Properties of the Dependent entity eg in the 'Column Mappings' bit map "PHOTO: byte[]" to the Value/Property called "PHOTO : byte[]"

No comments:

Post a Comment