Changing Namespaces in Entity Framework

If you are like me, migrating to entity framework from linq to SQL, I must say you have been spoiled. What seemed to be very simple has gotten much more complex. I find that Entity framework solved my persistence ignorance problem but brought me much more complicated problems to deal with. Here is one that took ages to find out

In linq to SQL, I can not only control the namespace for datacontext but also the code generated classes for entities generated by linq to SQL. Bellow is the snap shot that shows where I can make these changes.

image

In the above screen shot, you can see that I am given two options to namespace generation. First, I get to specify which namespace my datacontext gets generated in. Second to avoid name collision, I am also given the opportunity to specify where my code generated entities are created. The option of Entity Namespace can be used to specify the namespace where entities are generated. On changing that you can also confirm that linq to SQL designer performed what you told it do by going to the designer.cs file. Below example shows the preview.

image

When using entity framework, you get only one option for changing the namespace and that is changing the namespace for entity datamodel. Not sure what namespace changes it makes because when I change the value for the namespace, I still saw my NorthWindEntityDataContext in the same namespace and also my code generated entities in the default namespace. One change I noticed after changing the namespace is, some of attributes defined in the code generated classes were now pointing to my new namespace declared but I don't know how does that help me much because entities are still in the same namespace.

Below is an example that shows this behavior.

image

image

After struggling for half an hour, I finally decided to right click the edmx file and sure enough i was greeted with another namespace change that actually allowed me to define which namespace my CLR objects be generated in.

image

Hopefully some of you will save sometime if you cant see an option for changing the namespaces for code generated entities.

9 Comments

Comments have been disabled for this content.