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.

Published Friday, August 15, 2008 11:10 PM by zhirani

Comments

# funny wallpaper » Changing Namespaces in Entity Framework

Pingback from  funny wallpaper » Changing Namespaces in Entity Framework

# Link Listing - August 16, 2008

Sunday, August 17, 2008 1:00 AM by Christopher Steen

Link Listing - August 16, 2008

# re: Changing Namespaces in Entity Framework

Tuesday, August 19, 2008 9:25 PM by Phil Bachmann

Thanks for some useful information in this and other posts ... even if this information simply confirms my suspicion that the Entity Framework may be too new for production use.

# De namespace wijzigen in EF

Tuesday, October 07, 2008 11:53 AM by Jo-wen Mei

Standaard wordt de naam van de edmx file gebruikt als namespace voor de gegenereerde classes. Dit kun

# re: Changing Namespaces in Entity Framework

Thursday, January 01, 2009 12:28 PM by David Cater

Thanks for the helpful post!  Exactly what I was looking for.

# re: Changing Namespaces in Entity Framework

Wednesday, April 01, 2009 12:43 PM by weitzhandler

Thank you so much

That was so helpful!!!

# re: Changing Namespaces in Entity Framework

Tuesday, December 22, 2009 12:31 PM by Jason Kergosien

Zeeshan,

Congratulations on your book.  It was good to see you at DevConnections. I'm hoping to catch your next presentation, if I'm in town.

Regards,

Jason

# re: Changing Namespaces in Entity Framework

Thursday, January 28, 2010 10:43 AM by Vinney

Thanks so much. That was driving me nuts!!!

# re: Changing Namespaces in Entity Framework

Saturday, January 29, 2011 2:10 AM by heromyth

That's what I want, thanks!

# re: Changing Namespaces in Entity Framework

Wednesday, March 23, 2011 4:02 PM by Tobias

You made my day and saved my evening.. Thanks a lot!

# re: Changing Namespaces in Entity Framework

Friday, March 25, 2011 6:44 PM by sddaveb

FTW!!!!!!!  Thank you so much.  This has been driving me nuts all morning.

# re: Changing Namespaces in Entity Framework

Monday, June 06, 2011 10:23 AM by GBotros

Thanks

Leave a Comment

(required) 
(required) 
(optional)
(required)