> For the complete documentation index, see [llms.txt](https://raviram.gitbook.io/ef-core/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://raviram.gitbook.io/ef-core/basics-of-ef-core/create-the-project/create-the-model-and-context.md).

# Create the model & context

## Create the model `Student` <a href="#create-the-model" id="create-the-model"></a>

1\) open the project we just created in Visual Studio

2\) In the project root directory create a `Student` class as shown:

```csharp
namespace EFGetStarted
{
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public bool IsEnrolled { get; set; }
    }
}
```

{% hint style="info" %}
An entity framework model is nothing but a plain old C# object (POCO)
{% endhint %}

A real project model will have a lot many properties, but we have just shown a few here to for brevity. A model can also contain **navigation properties** to other models which we will discuss later.

## Create the context MyContext

Let us create an application context called `MyContext` as given below, in the root of the project

```csharp
using Microsoft.EntityFrameworkCore;

namespace EFGetStarted
{
    public class MyContext: DbContext
    {        
        public DbSet<Student> Students { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
            var server = "(localdb)";
            var instance = "mssqllocaldb";
            var database = "CollegeDB";
            var authentication = "Integrated Security = true";

            var conString = $"Data Source={server}\\{instance}; Initial Catalog={database};{authentication}";

            options.UseSqlServer(conString);            
        }
    }
}
```

### What is a DbContext ?

A DbContext instance represents a session with the database and can be used to query and save instances of your entities.&#x20;

* DbContext is a combination of the Unit Of Work and Repository patterns.&#x20;
* Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance.
* Typically, you create a class that derives from DbContext&#x20;
* This class then contains DbSet\<TEntity> properties for each entity in the model.&#x20;
* We Override the `OnConfiguring(DbContextOptionsBuilder)` method to configure the database&#x20;
* We can also configure other options to be used for the context.&#x20;
* The model is **discovered** by running a set of **conventions** over the entity classes found in the DbSet\<TEntity> properties on the derived context.&#x20;
* To further configure the model that is discovered by convention, you can override the `OnModelCreating(ModelBuilder)` method.
* It also has change tracking capabilities which we will discuss later&#x20;

{% hint style="info" %}
A DbContext instance represents a session with the database. The model is **discovered** by running a set of **conventions** over the entity classes found in the DbSet.
{% endhint %}
