Friday, September 19, 2008

Changing default namespace name for Entity Framework model / Изменение имени пространства имен по умолчанию в модели Entity Framework

As far as I started to deal with Entity Framework, which has been Beta for a long period and now is in Pre-Release I began to get in some issues with it. So I want to write about some of them, maybe it will save some time for you.
The beginning will be about the naming issue of automatically generated EDM classes. The problem is that the designer resets the name of the namespace for Context and Entities classes even if I write them explicitly (it does so after every validation of the model). The solution for this is to click on the EDMX file in the Solution Explorer, look at the properties, there's a "Custom Tool Namespace". Set this to be whatever you want the code-gen to use. But it was a surprise for me why model designer was ignoring "Namespace" property of the developed model. The answer from Microsoft stuff throwed the light on the difference between these two properties:

The Namespace property of the conceptual model is used in code attributes to map objects to the conceptual model.

The Namespace property of the .edmx file can be used to determine the namespace of the code generated by the Custom Tool. If no value is supplied, the namespace of generated code depends on the project type:

• For Visual Basic projects, the namespace of generated code will be the namespace specified in the Custom Tool Namespace property of the .edmx file if one has been specified. Otherwise, the namespace of generated code will be the root namespace of the project as specified in the project's Application Page. (If no value has been set for the Custom Tool Namespace property and the project has an empty namespace, the namespace of the generated code will be the conceptual model namespace)

• For C# projects, the namespace of generated code will be the namespace the Custom Tool Namespace conceptual model property if one has been specified. Otherwise, the namespace of generated code will be the default namespace of the project (as specified in the project's Application Page) plus the folder path of the .edmx file location. For example, if the path of the .edmx file relative to the project's root directory is Folder1/Folder2/Model.edmx, the namespace of the generated code will be ProjectDefaultNamespace.Folder1.Folder2.
• For ASP.NET Website projects, if the .edmx file is in the root of the App_Code folder, then the namespace of generated code will be the conceptual model’s namespace as defined in the CSDL section of the .edmx file (for more information, see Namespace Attribute (CSDL)). If the .edmx file is in a sub-folder of the App_Code folder, then the namespace of the generated code will be the folder path relative to the App_Code folder. For example, if the .edmx file is in App_Code/Folder1/Folder2, the namespace of generated code will be Folder1.Folder2


No comments: