Entity Framework additional notes part 1

beansproutscompleteSoftware and s/w Development

Dec 13, 2013 (3 years and 6 months ago)

68 views

.NET Database Technologies:

Entity Framework

additional notes


part 1

“Making the conceptual level real” (Blakeley, 2006)


Raises the level of abstraction from the logical (relational)
level to the
conceptual

(entity) level


Provides a
data model
that can define the structure
essence of entities


Entities

and
relationships

as first
-
class objects


Provides a
concrete

representation

that allows us to
manipulate data at this level


Entity Data Model (EDM)


relates to Entity Relationship
Model (Chen, 1976)


The EDM is a
value
-
based

conceptual model
-

it does not
incorporate behaviours of any kind


Compare with UML, which expresses a conceptual model
incorporating behaviour

Using an EDM


EDM consists of:


Conceptual model


Logical model (storage in relational database)


Mapping between these


EDM can be used to generate C# (or VB) classes which
can be used in an application


EF provides services for queries which work with C#
objects


EF manages mappings from database data to object
properties

Benefits of using an EDM


Data model in application is not tied to database schema


EDM allows developer to shape the data to suit the
application, not the database


Database may be a shared or legacy db which cannot be
modified by application developer


Normalisation or other optimisations in db may produce
schema which does not match the application well


Compare with Linq to SQL, which is (essentially) designed
to allow strongly typed querying of relational database

So, it’s an ORM then?


EF v1 allows building EDM by generating from database
and manipulating in EDM Designer


Allows building of EDM and configuring mapping to
databases


In both cases, EDM Designer generates code to represent
data model in application


Not intended to take existing domain model and map to
relational database


Not really an ORM, although it has many of the
characteristics of one


EF v4 moves significantly towards being an ORM

EF v1 and persistence ignorance


EF v1 entity classes require a number of non
-
PI features,
including:


Inherit from a certain base class
-

EntityObject


Only instantiate via a provided factory


Use specially provided datatypes, such as for collections
-

EntityCollection


Not PI!

EDM Mappings
-

Basic


Map entities directly to tables


EDM has navigation properties


Can rename properties, exclude properties as required

EDM Mappings


Table Splitting


Two entities map onto the same table


Entities have one
-
to
-
one relationship

http://thedatafarm.com/blog/data
-
access/ef
-
table
-
splitting
-
ndash
-
the
-
opposite
-
of
-
entity
-
splitting/

EDM Mappings


Inheritance (Table per Hierarchy)


Base class and derived classes all map to the same table


Discriminator field indicates which type to instantiate

EDM Mappings


Entity Splitting


One entity maps onto two separate tables


Tables must share a common key


Can access all properties in code without using navigation
properties

EDM Mappings


Inheritance (Table per Type)


Base class and derived classes map to the separate tables


Tables must share a common key

EDM Mappings


Complex Types


Refactor set of properties of an entity to a complex type


Can use complex type in other entities


Complex type can only be persisted as part of an entity