I'm doing some refactoring and am trying to reuse my genertated entity models. My application has a few assemblies, one being my outward facing public types (API) and one containing implementations of providers (such as the log).
I'd like to split the generation of the entities and models so that the entities will be in the API assembly and the container will be in the implementation assembly. Is this possible?
Is possible. This is how I did it.
- Assembly A
- Database.EDMX
- Models.TT
- Models.cs
- Assembly B
- Database.EDMX (Added as a Link to the real file in Assembly A)
- EntityContainer.TT
- EntityContainer.cs
That's how everything is laid out. These are the rough steps:
- Right click on the EDMX in A (public API assembly) and Add Code Generation File
- Adds a TT to the project. Called it Models, as it will contain the models only.
- Edited the TT and removed code generation for entity containers
- In assembly B (internal implementations) added Database.EDMA as a link
- Opened in assembly B, right click and Add Code Generation File
- Adds a TT to project B. Called it EntityContainer as it will contain that only.
- Edited TT to do the following
- Removed entity creation steps
- Changed the path to Database.EDMX to a relative path pointing at the original copy in A
- Added a
using
for my models
Hopefully this will all compile and work correctly (I'm still far from getting everything compiled and tested). Looks good so far.
Additional change:
In my entity container TT, I had to modify the definition of the EscapeEndTypeName to the following:
string EscapeEndTypeName(AssociationType association, int index,
CodeGenerationTools code)
{
EntityType entity = association.AssociationEndMembers[index]
.GetEntityType();
return code.CreateFullName(
code.EscapeNamespace(association.NamespaceName), code.Escape(entity));
}
I'm using association.NamespaceName
as it contains the correct namespace from the other assembly.