Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a "Model" and a "Context" in Entity Framework jargon?

What is the difference between a "model" and a "context" in Entity Framework jargon?

I'm using the Entity Framework database first approach in an application. These terms have come up many times as I've been reading different forums and articles on EF implementation strategies. I can't seem to figure out how these two are different (not even with just entity framework, but with software development in general). People use the words as if they are different, but then some people seem to use the words interchangeably.

like image 277
user1431072 Avatar asked Sep 05 '13 17:09

user1431072


People also ask

What is context in Entity Framework?

The context class in Entity Framework is a class which derives from System. Data. Entity. DbContextDbContext in EF 6 and EF Core both. An instance of the context class represents Unit Of Work and Repository patterns wherein it can combine multiple changes under a single database transaction.

What is the difference between model and entity?

Entity: 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. Model: A model typically represents a real world object that is related to the problem or domain space.

What is difference between DbSet and DbContext?

Intuitively, a DbContext corresponds to your database (or a collection of tables and views in your database) whereas a DbSet corresponds to a table or view in your database.

What is model in Entity Framework?

The Entity Data Model (EDM) is a set of concepts that describe the structure of data, regardless of its stored form. The EDM borrows from the Entity-Relationship Model described by Peter Chen in 1976, but it also builds on the Entity-Relationship Model and extends its traditional uses.


2 Answers

Context

This is easy. The context is either the DbContext or the older ObjectContext class that is the core of the entity framework data access layer. It supplies transparent database access through strong typed sets of entities, tracks and saves changes, manages database transactions and connections, and contains a number of utility methods to facilitate all kinds of data access tasks (esp. DbContext).

Model

This can be two (or three) things.

  • The data model, or store model. Which is the relational model of the database underlying the EF data access layer.
  • The conceptual model, or class model. Which is the .Net class model that represents the database. This model can either be generated by EF (database-first) or it can be an existing class model (code first). The conceptual model and the store model are linked through mapping, so EF knows how to populate .Net classes from database records and, conversely, how to save .Net classes to the database.
  • Some people refer to the classes in the conceptual model as "models". This is not wrong, but I prefer to use the name entities for this.

So context and model are two quite different things. You might say that the context is the middleman between two different types of models.

like image 165
Gert Arnold Avatar answered Sep 24 '22 19:09

Gert Arnold


Loosely speaking a context relates to a database connection or session wherea the model is the mapping between tables, views, etc to data access object classes (i.e., objects that will contain the data)

like image 25
Otávio Décio Avatar answered Sep 24 '22 19:09

Otávio Décio