EntityFramework 3.5: Adding a New Entity to an Existing .Edmx File

It's been awhile since my last blog posting. To rekindle the *blogging fire* I want to share a little trick that I learned today that will hopefully help the community at large (or at least those EntityFramework fellow compatriots).

The EntityFramework is a great technology framework that raises the level of abstraction for data programming. It offers a great graphical user interface for defining either simple or complex representation of your data. While the GUI is a great tool, some of the kinks have yet to be worked out of the 3.5 version of the framework.

And while it is extremely simple to create a new edmx file and define your entities, this posting will not cover how to do this. I will try to zero in on the *trick* that I learned today

Define the problem:

While creating a new edmx file and defining the entities is easy, it's a whole 'nother animal to update and existing edmx file. What would seem rather simple is not quite as straight forward in this version of the framework (at least in my experience). It would appear that you would only need to open the edmx file, right click on its design surface, and select the "Update Model from Database" menu item. I mean, after all, this launches a nice little "Update Wizard" that allows you to select the tables that you either want to add, update or delete.

Not so fast my friend! If you have an edmx file that has a modicum of complexity I have found this to be a painfully frustrating way to add a new data model to the file. I have spun many-a-cycle and invested many hours in trying to coerce the wizard to play nice, only to alas, nearly fall down, quivering in humble defeat.

But, my pride never allows me to succumb to such a challenge... defeat isn't an option!

What I have done in the past to add a new entity to an existing edmx file was to right click on the edmx file in Visual Studio Solution Explorer and select the "Open With" menu option. This opens the edmx file in an XML editor. I would then painstakingly  go through the file and add all of the appropriate entity definition declarative code that would not only define the conceptual .Net entity, but also define the data store and how it would be mapped over to my .Net entity.
This was a long, arduous process, and highly prone to error. I knew that there had to be a better way...

Trick/Lesson Learned:

Well, today I had an epiphany (or at least I like to think it was). Instead of fretting over the quirkiness of using the less that effective "Update Wizard", I decided to take a different approach...

I created a new edmx file (again, this posting does not describe the process of creating and edmx file) and added to this new edmx file only those entities that I eventually wanted to add to my existing edmx file. This allowed me to rely on the framework's creation wizard to create and edmx file. This wizard is rock solid, so why not allow it to generate all of the XML that I will eventually need to add to my existing edmx file; because after all, all an edmx file is, under the covers, is an XML file...

After creating the new file with the creation wizard, I then opened it up in the XML Editor. Then, after opening the newly created file in the XML Editor, I opened up my existing edmx file in a separate XML Editor (this is the file to which I wanted to add the new entities).

At this point, it was very straightforward: I simply added all of the appropriate XML markup from the newly created edmx file to the existing edmx file. It took only a matter of minutes.

Woohoo!!

Conclusion:

While there may be a more reliable way to add new entities to an existing edmx file that I'm not aware of, I have found that the current "Update Wizard" is less than reliable...and sometimes you need to roll up your sleeves and work with some good 'ol XML. And a surefire way to get it right first time, is to create a staging edmx file whose only purpose in life is to act as a surrogate for your existing edmx file. You can then use this staging file to add the appropriate XML to the existing edmx file, and continue on with your day.

Happy coding!

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.