Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 5 namespaces

I want to add Entity Framework 5 database first into a class library in Visual Studio 2012 targeting .net framework 4.5. I am confused with all the labels I need to key in:

  • The EDMX file name when adding ADO.NET entity data model to the project. I put ‘MyEF.edmx’.
  • When saving the connection string into the config file. I put ‘MyEntities’.
  • After selecting some tables to include in my model, there’s a textbox to input model namespace. I put ‘MyModel’.
  • Property ‘custom tool namespace’ of the MyEF.edmx file. I put ‘TheEF’.
  • Property ‘custom tool namespace’ of the MyEF.Context.tt file. I put ‘TheContext’.
  • Property ‘custom tool namespace’ of the MyEF.tt file. I put ‘TheModel’.

Opening MyEF.edmx with ADO.NET entity data model designer, looking at the properties of MyModel, there are:

  • entity container name, filled with ‘MyEntities’. So the connection string name goes here.
  • namespace, filled with ‘MyModel’. This is coming from the table selection textbox.

Putting something into the edmx custom tool namespace doesn’t seem to do anything. I got this conclusion because when I grep the entire source code folders, I found it only in a vbproj file.

Putting ‘TheModel’ into MyEF.tt custom tool namespace produces error from MyEF.Context.vb saying type ‘MyTable’ (this is the name of my database table) is not defined.

Can someone explain the purpose of each label?

If I want to put all the classes generated by this one edmx (DbContext, models, etc.) into one namespace, ‘MyEF’, what should I put in each of those places?

like image 792
Endy Tjahjono Avatar asked Jan 28 '13 11:01

Endy Tjahjono


1 Answers

The various properties are used as follows:

  • EDMX file name --> Used for the EDMX file name
  • Connection string name --> Used for the connection string name in the config file, and also for the container name of the conceptual model (CSDL) part of the EDMX
  • Model namespace --> Used for the namespace of the conceptual model (CSDL) part of the EDMX, and also for the store model (SSDL) part with .Store appended
  • Custom tool namespace for the EDMX file --> I don't believe this is used for anything when using T4 generation of POCO entities. When using EF1-style built in code generation, setting this property will set the .NET namespace for all generated files.
  • Custom tool namespace for .Context.tt file --> The .NET namespace used in the source file for the context
  • Custom tool namespace for .tt file --> The .NET namespace used in the source files for the entities

Note that if you set the .Context.tt and .tt custom namespaces to different things, then the context will be generated in a different namespace to the entity types and this won't compile out-of-the-box. You can update the .tt files if you want to use different namespaces here, but more often people just use the same namespace for both.

Also note that you may need to choose "Run Custom Tool" from the context menu for each .tt file after changing the properties in order for the code to be re-generated.

like image 137
Arthur Vickers Avatar answered Sep 20 '22 18:09

Arthur Vickers