Programming Entity Framework

.Programming Entity Framework


Programming Entity Framework


Web page by Kevin Harris of Homer IL

Please contact Kevin Harris of Homer IL concerning this web site

"The bible of Entity Framework written by Julia Lerman. Weighting in at 900 pages, the second edition covers Entity Framework at a point when many crucial updates were added at .Net 4. While SQL is the established data access technology for databases, Entity Framework is where Microsoft is making all of its innovations."

This page is a review of the book, with additional notes, as I read through the chapters. I have worked with EF for a couple of years, creating and maintaining a few production applications using C# and EF 5 and EF 6. There are times when I like the ability EF provides to abstract away the database details and view application challenges in terms of programming objects and domain models. However there are other times when working directly at the SQL level provides the database perspective of data needed for handling unique data situations with a finer degree of control.


Chapter 1: Introducing the ADO.NET Entity Framework

Entity Framework (EF) provides an abstraction layer between applications and various databases. This allows for easier object-oriented programming (OOP) against a domain model of the data instead of the underlying physical structure of the database. With EF the code more directly addresses the business domain than the normalized database schema. Additionally it allows working with more powerful commands which can be used against a variety of database management systems. Instead of coding queries full of inner and outer joins, EF uses navigation properties to easily access related table with a simple dot notation. EF supports LINQ queries and Lambda expressions which are more terse, and in many cases, more powerful, than SQL. They have the benefit of consistent usage and syntax regardless of database type. EF also removes tedious programming tasks such as handling database connections and updating specific database tables. Changes are saved to the domain model and EF tracks the data which really changed and performs the individual table updates using the proper relationships and constraints.

After expressing all the virtues of Mircrosoft's EF, there are credits and limitations which need to be stated. Microsoft was not the first to notice the design mismatch between OOP and relational databases. EF is a later version of Object-Relational-Mappers (ORM) following more established ORMs such as Hibernate. ORMs themselves have evolved from the concept of Entity Relationship Modeling (ERM) which goes back more than 30 years. Courses in database design usually contain practice in creating entity relationship models which reflect the schema of the business objects, not the schema of database tables. EF is limited to the databases which provide it support. While the list of databases supported is long (Oracle, DB2, Sybase, Informix, Microsoft SQL, MySQL, PostgreSQL, ...), there are still some limits. Such as EF does not provide access through generic ODBDC drivers nor is it supported in Access.

Also the first version of EF, in .NET 3.5 SP1, was not embraced by the programming community. It was not until the second version of EF (EFv4) released with .NET 4.0 (Visual Studio 2010) that EF eliminated some painful restrictions and design weaknesses that enabled it to be viewed as viable technology for the industry. EF has evolved with each new version (EFv5, EFv6) and has gained mainstream support. Microsoft continues to increase its support for EF in all of its products and states that all the innovation is going into EF, not the older SQL technology. However the older ADO.NET SQL technology is still the more appropriate solution in some cases. It is widely used by the industry and will not be going away in the foreseeable future. ORMs are not a replacement for SQL, but are an alternative tool which can improve the speed of application development and ease maintenance efforts by abstracting away database details.

The version of EF currently being developed my Microsoft (EFv7) is supporting Microsoft direction to run on most platforms (include Linux, OSX, and others). EFv6 and EFv7 will continue parallel development and both be supported. EFv7 is entirely open-source on GitHub and is a completely new codebase designed to be more efficient, extensible, and flexible. It will support a wider range of relational and non-relational (NoSQL) data stores (giving it more capability than an ORM which only works with relational data). More information about EFv7 is provided by Julia Lerman in this .NET Rocks! webcast.


Chapter 2: Exploring the Entity Data Model

EF contains an Entity Data Model (EDM) which provides the ability to work with a conceptual view of the data instead of the actual database schema. This section looks at what an EDM is, how it is created, and how to work with it.

The EDM is defined in single file containing XML, which has a file extension of .edmx. Within the file are three sections which are broken out and processed separately at run time. The first section defines the physical schema of the database. The second section contains the conceptual view of the data. While the third section contains the data mapping between the physical schema and the conceptual view.

The EDM is automatically created by EF using one of three separate approaches. The initial release of EF only supported the database-first approach which used and existing database to create the EDM. The second version of EF in .NET 4.0 added support for the model-first approach which allowed a data model to be designed with a graphical tool. EF would then use the model to create the EDM and generate the database. In .NET 4.1 EF introduced the code-first approach which used the data class objects (created in code) to create the EDM and generate the database. Most of my experience has been with the database-first approach because I either have a legacy database or coworkers feel more comfortable designing the database themselves. However the code-first approach is gaining popularity. Many coding demonstrations and rapid development projects use code-first because they can skip over the database design and creation and concentrate on the coding.



Error | ASP.NET Developer

Error

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /srv/disk9/1218369/www/kcshadow.net/aspnet/includes/common.inc:2748) in drupal_send_headers() (line 1232 of /srv/disk9/1218369/www/kcshadow.net/aspnet/includes/bootstrap.inc).
  • PDOException: SQLSTATE[42000]: Syntax error or access violation: 1142 INSERT command denied to user '1218369_b2cf'@'185.176.40.58' for table 'watchdog': INSERT INTO {watchdog} (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9); Array ( [:db_insert_placeholder_0] => 0 [:db_insert_placeholder_1] => cron [:db_insert_placeholder_2] => Attempting to re-run cron while it is already running. [:db_insert_placeholder_3] => a:0:{} [:db_insert_placeholder_4] => 4 [:db_insert_placeholder_5] => [:db_insert_placeholder_6] => http://www.kcshadow.net/aspnet/?q=programmingentityframework [:db_insert_placeholder_7] => [:db_insert_placeholder_8] => 54.162.171.242 [:db_insert_placeholder_9] => 1534727954 ) in dblog_watchdog() (line 160 of /srv/disk9/1218369/www/kcshadow.net/aspnet/modules/dblog/dblog.module).
The website encountered an unexpected error. Please try again later.