Changing the Namespace on generated Entity Framework classes by ThinqLinq

Changing the Namespace on generated Entity Framework classes

As I was preparing a presentation recently, I started hitting my head into a brick wall when trying to change the namespace on the entities generated by the Entity Framework. Having spent so much time with LINQ to SQL, I was anticipating that the behavior would be similar enough to make this easy. Unfortunately, I was mistaken.

First, what I tried to do wrong. With LINQ to SQL, if you click the unused design surface, you can set the namespace for the Context separately from the Entities through the property window of the dbml file.

The Entity designer window with the edmx file also has a namespace option in the property window. In this window you can set the Namespace for the ConceptualEntityModel's Schema. Notice from the image here, that the help indicates that this should be "The namespace for the Entity Data Model." Unfortunately, this does not mean that it will be the namespace used by the classes that the code generation tool uses when it generates the actual VB or C# classes that represent the conceptual model.

After jumping back and forth between the XML and classes and the Class View, trying to build and rebuild the project, I got very frustrated because the namespace that I specified appeared in the EDMX file, but the generated classes were not appearing with the correct namespace. I finally gave up and sought out a bit of help. Shawn Wildermuth came to the rescue and pointed me to the property window of the NWindModel.Edmx file itself rather than the the property window of an area in the design surface.

On the property window for the .Edmx file we need to set the value for the Custom Tool Namespace. Once that is done, our classes will be generated in the namespaces that we expected.

Looking a bit deeper at this provides some interesting information. The LINQ to SQL dbml file saves the EntityNamespace and ContextNamespace in the Database element:

<Database Name="Northwind" EntityNamespace="Linq" ContextNamespace="Linq" Class="NWind2DataContext" xmlns="">

In contrast, the namespace that we set on the custom tool namespace is not included within the EDMX at all. Instead, it is contained in the actual project file (vbproj or csproj):

EntityDeploy Include="NWindModel.edmx">

Posted on - Comment
Categories: Entity Framework) - LINQ) -
comments powered by Disqus