What's in an Entity Framework namespace?

That which we call a namespace by any other name would organize our class hierarchy.

When creating an Entity Framework model the add item wizard prompts you for two names.

The first is a text box on the "Choose your data connection page." It is at the bottom under a check box labeled "Save entity connection settings in App.config as:". The name defaults to your database name with "Entities" appended to it. If you uncheck the check box this text box grays out not allowing you to edit the value. This value is used as the name in the connectionStrings section of an app.config which is added to your models project if it doesn't already exist.

The second is on the "Choose your database objects" page of the wizard there is a text box labeled "Model NameSpace" which defaults to the name of your database with "Model" appended to it.

Once the model is generated if you open it in the designer and click on the background the properties window shows that you are looking at the properties for the DatabasenameModel (ie, the name you provided as the "Model Namespace"). There are two properties which contain the two names that were entered in the wizard as described above. "Entity Container Name" and "Namespace". By the way, MS calls this the "Entity Container".

"Entity Container Name" will contain the name that you specified on the "Choose your data connection page". Well this is very intuitive, NOT. I thought that was the name that identified the entity connection string like the wizard told me. Well read on.

"Namespace" will contain the name that you specified on the "Choose your database objects" called the "Model Namespace". Well, at least it's called namespace in both places.

Now, take a look at the generated code. It will be in the (yes the models file name can also be different) ModelName.Designer.cs (or vb) file. The first code think you see will be something like:

namespace ConsoleApplication1

Well, wait a second why isn't it DatabasenameModel or ConsoleApplication1.DatabasenameModel? That is what is specified as the namespace. Well, dear reader the namespace value on the model is nowhere in the generated code.

Now the entity container name and the connection string name which is by default DatabasenameEntities is the name given to the generated class which derives from ObjectContext. By default the constructors of this generated class expect the connection string to be stored with a name that matches this container name. So, if you change the container name be sure to modify the connection name in your config or you will get some fun errors.

BTW: If you unchecked the "Save entity connection settings in App.config as:" check box the entity container the entity container will be named "Entities".

So, what the heck is the "namespace" used for? This is used in the conceptual schema. No, not as an xml namespace, thanks for playing, but as an identifier in the model pre-pended to all the entities, relationships, etc. Apparently you can use this name in Entity SQL to disambiguate it from other containers. The EF doc on namespaces is quite sparse.

The bottom line is that these values in the wizard are a bit overloaded. I don't understand why they don't specify on the wizard that you are choosing the name for the Entity Container/Derived ObjectContect class rather than the connection string's name token.

I have chosen to use either DatabaseNameContext or DatabaseName (still not sure) for the entity container name since in my code this is considered the context class. In my code I work against the generated class names and know this to be my context. The default name of DatabasenameEntities doesn't seem correct. Because it really isn't the entities although it doesn't contain helper methods to create entity sets.

I have choose to use DatabaseNameModel for the namespace. You really don't use this name unless you write entity SQL, and even then it seems optional.

I hope this helps someone out a bit. I'm sure if I have something wrong here I'll hear about it.

The bottom line is, wag of my finger to Microsoft for calling these values by different names at different places.

Comments

Pingback

[...] What's in an Entity Framework namespace? | No Long Days bob.archer.net/content/whats-enity-framework-namespace – view page – cached That which we call a namespace by any other name would organize our class Tweets about this link Topsy.Data.Twitter.User['pilotbob'] = {"location":"Tampa, Florida, USA","photo":"http://a1.twimg.com/profile_images/52011836/gravatar_normal.JPG","name":"Bob Archer","url":"http://twitter.com/pilotbob","nick":"pilotbob","description":".Net developer, former VFP developer future Ruby developer.","influence":""}; pilotbob: “Blogged: What's in an Enity Framework namespace? (http://bob.archer.net/node/31) ” 11 hours ago view tweet retweet Filter tweets [...]