Wednesday, 25 March 2009

Entity Framework Complex Types (IDE Issues)

Have recently been getting to grips with the Entity Framework and MVC, both of which I’m using on a relatively small project to gain an understanding of the technologies and to evaluate whether we should consider either of these technologies for more major solutions at this stage in their life-cycle.

There’s obviously a fair old learning curve involved, but armed with Julia Lerman’s book have made good progress with Entity Framework and thus far have been very impressed. Have run into a few issues of course, and the one giving me most grief is regard to complex types.

The issue arises when, having created your model from your database you want to make some customisations to it. This is an important feature, as one of the major points of this technology should be to allow you program against a model that you (the app developer) want to use and not be completely tied to the database structure.

A common requirement here would be to convert a single table (or imported entity) into two – the entity plus another one represented as a property of the first. The classic example would I guess be an address.

Say you have a table Customers with the following fields:

ID
FirstName
LastName
AddressLine1
AddressLine2
City
County
Postcode

And want to represent this as two classes – Customer and Address.

Customer
ID
FirstName
LastName
Address

Address
Line1
Line2
City
County
Postcode

This would allow you to refer to the address of an instantiated Customer object like this...

objCustomer.Address.City

... which is arguably more elegant and more practically would allow you to re-use the Address class elsewhere.

This is supported by the Entity Framework, but not by the designer – you have to make the following amends in the XML directly.

I can live with that as am happy to get my head around what is going on under the hood, but unfortunately this means the Designer support is gone for good – you have to make all subsequent edits in the XML. Hopefully this will be supported in future versions.

More annoyingly though, VS.Net reports a couple of errors (not warnings) that would suggest a build failure has occurred. It hasn’t though, it will build fine if you ignore these errors and can use the complex types as expected. But this makes working with the project rather tricky from then on, as you have to scroll past these “errors” to find your real ones as you continue working on the solution.

[Update 25/11/09: looks like this issue has been resolved with entity framework 4]

No comments:

Post a Comment