2
votes

Here is the senario for which I could not find anything useful. Maybe Im the first person thinking of doing it this way:

Approach: Database First

Database: SQL Server 2008 R2

Project : DLL (Data Access)

I have a data access library which encapsulates all the access to database as well as the biz functionality. The database has many tables and all the tables have the following 2 columns:

  • last_updated_on: smalldatetime
  • last_updated_by: nvarchar(50)

The project contains several models (or edmx files) which contain only related entities which are mapped to the tables they represent. Since each of the table contain the columns for last_updated_* I created a complex type in one of the models that is as follows:

Complex Type: History

  • By (string: last_updated_by)
  • On (DateTime: last_updated_on)

The problem is that it can only be used in the model in which I defined it.

A) If I try to use it in other model it does not show it in the designer B) If i define it in the other models I get error History already defined

Is there any solution so that the History complex type, defined in one model can be reused by other models?

1

1 Answers

0
votes

I was trying to do almost the exact same thing (my DB fields are "created", "creatorId", "modified", and "modifierId", wrapped up into a complex type RecordHistory) and ran into this question before finding an answer...

http://msdn.microsoft.com/en-us/data/jj680147#Mapping outlines the solution, but since it's fairly simple I'll cover the basics here too:

  1. First create the complex type as you did (select the fields in the .edmx Designer, right click, select "Refactor into New Complex Type")

  2. In the .edmx Designer (NOT the Model Browser) right click on another table/entity that has the same common columns and select Add -> Complex Property

  3. The new property will be assigned a complex type automatically. If you have more than 1 complex type, edit the new property's properties and set the Type appropriately

  4. Right-click on the table/entity again (in either the Model Browser or Designer) and select Table Mapping

  5. Update the Value/Property column for each of your common fields (changing them, in my case, from "modified : DateTime" to "history.modified : DateTime")

  6. Delete the old common fields from your entity, leaving just the complex type in their place