Author : HASSAN MD TAREQ

What is Entity Framework?

  • Entity Framework == .Net ORM Framework
  • Entity Framework is a set of technologies that supports development of data-oriented software applications by enabling developers to work with conceptual models that are mapped to logical schemas in data sources

What is Entity Framework Core?

Entity Framework (EF) Core is the lightweight, extensible, and cross-platform version of Entity Framework.

Model

  • A model typically represents a real world object that is related to the problem or domain space. In programming, we create classes to represent objects. These classes, known as models, have some properties and methods (defining objects behavior).
  • In MVC, the model ‘M’ is a layer (not a class or any single object) and . See: https://stackoverflow.com/a/5864000/4802664

The term “Model” is ambiguous. Following are all models

  • Entity Model: A class which closely resembles structure in persistence. A MemberEntity is a model which represents one member row in the Members table in a database. Not strictly tied to a Database, but some entity of some persistence. Typically has an “ID” property such as “int MemberID”.
  • ViewModel: A class which closely resembles structure on a View/UI. A MemberViewModel is a model which represents one member to be displayed on a Members View/UI on the frontend of an application. Not strictly tied to the MV* pattern.
  • Domain Model: A class which represents part of the problem domain. The MemberModel is responsible for its creation and validation. Because services take in and return out models, the models are responsible for their own business logic which validates their correct construction and usage. E.G.: A MemberModel should break if you try to use it without a UserName.

See: https://entityframework.net/knowledge-base/26563444/entity-vs-model-vs-view-model

VVI: Model in context of Entity Framework may mean: Entity Data Model (EDM)

Entity

  • Entity means a thing with distinct and independent existence
  • An entity is any singular, identifiable and separate object. It refers to individuals, organizations, systems, bits of data or even distinct system components that are considered significant in and of themselves.
  • The term is used in a number of programming languages/concepts, database management, systems design and other arenas.
  • Entity holds the data that is persisted
  • An entity represents a single instance of your domain object saved into the database as a record. It has some attributes that we represent as columns in our tables
  • An entity in Entity Framework is a class that maps to a database table. This class must be included as a DbSet type property in the DbContext class. EF API maps each entity to a table and each property of an entity to a column of that table
  • An entity is the representation of a real-world element within Object Relational Mapping (ORM) (i.e. Entity Framework). This representation will be mapped to a table in a database and its attributes will be transformed into columns
  • See: https://cpratt.co/entities-are-not-models/

Entity in different context

  • Entity is persisted in database => Table
  • A class representing an Entity in a app => Model
  • Entity in edmx diagram => conceptual model

Entity is the model (class) within ORM that is reprensting a table row in the database

  • Entity properties are table columns
  • DbSet is the table
  • DbContext is the database

Entity Type

Entity Type in EF edmx = Entity Class in app

Conceptual Model

An abstract specification for the entity types, complex types, associations, entity containers, entity sets, and association sets in the domain of an application in the Entity Framework. The conceptual model is defined in CSDL in the .csdl file.

Here a conceptual model in edmx diagram

A conceptual model in edmx diagram

Entity Data Model / Model

Entity Framework Model = EF model = Entity Data Model = Model A set of concepts that describe the structure of data, as entities and relationships, regardless of its stored form.

  • An EF model stores the details about how application classes and properties map to database tables and columns
  • Entity type: a .NET Framework class that represents an entity as it is defined in the conceptual model. Entity types may have scalar, complex, and navigation properties. Objects are instances of entity types. For more information, see Working with Objects.

A model that describes entities and the relationships between them. EF uses EDM to describe the conceptual model against which the developer programs. EDM builds on the Entity Relationship model introduced by Dr. Peter Chen. The EDM was originally developed with the primary goal of becoming the common data model across a suite of developer and server technologies from Microsoft. EDM is also used as part of the OData protocol.

There are two main ways to create an EF model:

  • Using Code First: The developer writes code to specify the model. EF generates the models and mappings at runtime based on entity classes and additional model configuration provided by the developer.
  • Using the EF Designer: The developer draws boxes and lines to specify the model using the EF Designer. The resulting model is stored as XML in a file with the EDMX extension. The application’s domain objects are typically generated automatically from the conceptual model.

More: https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/entity-data-model

Entity Framework Mapping

DataBase Table <===> Conceptual Model (.edmx) <===> Entity Class (domain class)

EF mapping - table | conceptual model | entity class (domain class)

EF mapping - table | conceptual model | entity class (domain class)

Context

A class that represents a session with the database, allowing you to query and save data. A context derives from the DbContext or ObjectContext class.

  • Entity is table row
  • Entity properties are table columns
  • DbSet is the table
  • DbContext is the database

Entity Framework Aproaches

  • Database First: Creating an Entity Framework model, using the EF Designer, that targets an existing database.
  • Model First: Creating an Entity Framework model, using the EF Designer, that is then used to create a new database.
  • Code First: Creating an Entity Framework model using code. The model can target an existing database or a new database.

Lazy loading

A pattern of loading related data where related objects are automatically loaded when a navigation property is accessed.

Eager loading

A pattern of loading related data where a query for one type of entity also loads related entities as part of the query.

More Terminologies