I should call this post “Learning the practical side of ADO.NET Entity Framework” since it’s the first time I use it in a real world application. I generally do not like to use new technologies for the sake of it. However, this seemed to be the right time and opportunity to learn ADO.NET Entity Framework in a real world situation. A quick fix to a complex problem, I needed to “plug-in” a new web application into a legacy database, the catch was that I would have to change the database latter and this new database would have a new schema. ADO.NET Entity Framework would allow me to create my new schema with the old one and map it to the new database latter. Great to avoid to do the work twice…
The first thing I found out with the ADO.NET Entity Framework is that it is fiddly at best. I thought it was going to be the easiest solution and to be entirely honest had I coded my DAL myself as I used to, I’d be finished!
Right, I am not entirely truthful, I am using a database with garbage in it. But still, if I have to use NULL’s to differentiate between object types it becomes a problem, I do not believe in NULL values in a database.
The situation is as follows, I have a legacy table that contains product categories and actual products, the table’s data is really dirty. So I want to create 2 Entity Objects one called “Categories” and another called “Products” and let the ADO.NET Entity Framework do the differentiation using a column with a true or false, easy! well it does not work! I kept getting a mapping error in Visual Studio 2008. After some research on the net I found out, to my dismay, that differentiators can only use NULL’s. So, the product category record must have a NULL value in some arbitrary field to be recognised as a category object by the ADO.NET Entity Framework, how ridiculous!
So, I cleaned the data and set a field to NULL, set the clean renamed CategoriesnProducts object to Abstract = True. Next, I created 2 objects Categories and Products both inheriting from CategoriesnProducts then mapped my legacy table to those new entities. At which time I found that all the fields in the legacy table where flagged Not NULL, so I took those flags off, even though it meant changing the database, I knew that it would not affect any of the other applications. Refreshed the schema. Sort out the new entities mappings and voila… I had a successful, error free build.
On the bright side, a many-to-many relationship is a breathe in the ADO.NET Entity. It maps it all out and abstracts it completely as seen in the relationship between CategoriesnProducts and LegalDocuments.
Next Linq-to-Entities.