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.
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.
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.
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.
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.
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.
So context and model are two quite different things. You might say that the context is the middleman between two different types of models.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With