The Model First approach is useful when we don't have a database to start with. Using this approach, we can create our models directly in the Visual Entity Designer, and then create the database schema based on our model. In this approach too, we can create entities and their properties, define relationships and constraints between the entities, and create inheritance relationships.
In this approach, we will first create the conceptual model, and based on this the database and the strongly typed objects will be created. To start with the Model First approach, we first need to add an ADO.NET EDM.
Let's try to add an ADO.NET EDM in our application:
Since we are planning to start with an empty model, we have to select an empty model in the wizard. The following screenshot shows the wizard step that asks you to select the option. We have to select Empty model in this step:
Once we choose the empty model, Entity Framework will show us the Visual Entity Designer. Now we can add the entities and relations to the designer area from the toolbar. The Entity Designer and the toolbar will look something like this:
Let's try to add a new entity to our ToDo
application and add properties to that model. Let's add a few properties to our ToDo
model:
Let's add the scalar properties needed for our ToDo
model. Let's make the ID field a primary key field, a string property for ToDoItem
, and a Boolean property for IsDone
:
The created entity will look something like this:
Once we have the conceptual model created for our application, we have to create the database from this conceptual model. This can be done by right-clicking on the entity and choosing Generate Database from Model:
This will prompt you to select the database connection. This wizard will not create the actual database, but it will generate the DDL for us, and we will have to use that DDL to create the database ourselves:
After we select the database connection, the wizard will give us the SQL to create the database on the specified connection:
We can now copy this SQL and create the database. The Entity Framework has already created connectionString
to use this connection, and wired up the DBContext
class to perform the operations on this database. Let's take a look at the generated DbContext
class:
public partial class ToDoModelContainer : DbContext { public ToDoModelContainer() : base("name=ToDoModelContainer") { } public virtual DbSet<ToDo> ToDoes { get; set; } }
The strongly typed objects will also be generated from the conceptual model. Let's take a look at our generated ToDo
class:
public partial class ToDo { public ToDo() { this.IsDone = false; } public int Id { get; set; } public string ToDoItem { get; set; } public bool IsDone { get; set; } }
If we are using the Model First approach, one important thing to consider is that the incremental changes in our model will not perform the incremental updates to the database. Rather, it will give us the SQL to create the updated database from the ground up.
Tip
If we want to perform incremental updates to the database, we can use the Database Generation Power Pack from Microsoft (https://visualstudiogallery.msdn.microsoft.com/df3541c3-d833-4b65-b942-989e7ec74c87/). Using this, we can perform incremental updates to the database.