18

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.

1
  • The DbContext class is the base class that allows for database querying used in EF. The model in the MVC sense can refer to the general domain that includes entities (also named models) and database connections or might refer to a model, in which they basically mean a class that is used to represent your data (for example Person). Sep 5, 2013 at 17:16

3 Answers 3

13

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.

2
  • Considering bullet point #2 from your answer, what would be the reason why anyone would ever have more dbContexts than models and vice-versa? Is there a best practice when it comes to the number of models (edmx's) to the number of contexts used? Sep 5, 2013 at 21:05
  • 2
    @user1431072 There can be reasons to have separate contexts each representing distinct parts of the database. For instance, if there are separate tables for authorization you could have an authorization context (with accompanying authorization class model) and a business context. More (store) models than context is not possible: a context accesses one database. Sep 5, 2013 at 21:14
3

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)

3

A model is a class which usually represents a database table or structure to display a database table. For example, if I had a database for cars, then a model for car could be

public class Car
{
 [Key]
 public int CarId { get; set; }
 public string Make { get; set; }
 public string Model { get; set; }
 public int Year { get; set; } 
}

This model is used by entity framework and the sql provider (for mysql or mssql usually) to compose a query against a database. The query requires a way for this to be mapped, and that is the job of the context. The context usually extends DbContext and is what is used for the facade of accessing the database table as an in memory object.

public class CarContext : DbContext
{
 DbSet<Car> Cars { get; set; }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.