Posts Tagged ‘ADO.NET Entity Framework’

ADO.NET Entity Framework - my “getting started notes” - Part One

Thursday, July 10th, 2008

I am just getting started with the ADO.NET Entity Framework myself and I thought I’d share things about the experience as I go. Who knows, it might be of use to someone out there! Please, if you see something here that is not correct then let me know.

I found the following article a useful read for starters, although some of the detail is a little out of date I think. It’s a good overview if you need one:

http://msdn.microsoft.com/en-us/magazine/cc163399.aspx

Visual Studio has a visual designer to build entity models and it generates a single EDMX file which it will generate code from using the EntityModelCodeGenerator custom tool.

There is a project called “Sample EDMX Code Generator” (source code available):

http://code.msdn.microsoft.com/sampleedmxcodegen

which can also generate code from EDMX files. The project overview says:

The goal of the Sample EDMX Code Generator is to provide you with enough insight into how the ADO.NET Entity Designer generates code in Visual Studio and hopefully give you a head start with some sample source code.
SampleEdmxCodeGenerator is not intended for production use; instead, it demonstrates custom tool extensibility via SingleFileGenerator, code generation APIs, code generation events and EDM metadata APIs. The sample is also somewhat incomplete on error handling and has not been tuned for performance, stress, etc.

The Entity Framework Samples

http://code.msdn.microsoft.com/EFQuerySamples

project uses separate entity model files however:

·         Conceptual - CSDL

·         Mapping - MSL

·         Logical – SSDL

In VS these don’t appear to have a designer and so one cannot see a visual representation of the model in VS.

It would appear that the EDMX file combines the CSDL, MSL and SSDL files into one file. The XML namespaces (xmlns) in the EDMX file sections are the same as those in the individual files. It also contains additional sections used by the VS designer.

The CSDL defines the entities and the relationships as the application’s business layer will know them. The EntityModelCodeGenerator custom tool will generate code, classes from the CSDL, that represent the domain model. 

In VS the custom tool can be invoked manually from the context menu of the EDMX or CSDL file in solution explorer and or a separate CSDL file - select “Run Custom Tool” - it will normally be set to run the  EntityModelCodeGenerator custom tool, although you can of course change this.

SSDL

The SSDL file or the SSDL section of the EDMX file defines the structure of the relational data in the database.

MSL

The MSL file or the MSL section of the EDMX file contains the direct map from the CSDL to the SSDL.

The CSDL, SSDL and MSL are collectively called the EDM – Entity Data Model.

If you generate an EDM from a database and immediately open the CSDL and SSDL files without making modifications to them, you will find that they are similar because the models are generated directly from the database and the conceptual model is mapped directly to the logical store.

This, of course, does not demonstrate the capabilities of the EDM. At this point the EDM looks very similar to what we would have when creating entities and mappings for LINQ to SQL – just a one to one mapping. LINQ to SQL entity mapping is capable of doing a little more; it supports Table per Hierarchy (TPH), where multiple classes in a hierarchy are mapped to a single table using a discriminator column to determine the specific type of each row/instance (this is a very common database inheritance mapping). One can do this using EF also of course. However, we can see that the EDM allows us to separate the conceptual model from the database model and to connect the two using a mapping.

The real power of the EDM from the OO programmers point of view, then, is dependent on its mapping capabilities. I will be looking at Just how close our conceptual model can be to our ideal OO design for a business domain and yet be usefully mapped to the data model with EF. To me that is the key to EF and it’s usefulness.

Clearly EF intends to go further than the current crop of OR mapping tools in that it is clearly aiming at bridging the object to relational storage impedance mismatch in a different way.

How well it does this remains to be seen…………!!