Getting Strated with Model First Development using Entity Framework.
What is Model First ?
In Model First approach you will create a model of your domain entity using the EF designer and then generate database schema based on it. It stores the meta data in a EDMX file and model classes that interact with our business objects are automatically created from this EDMX file.
Let’s create a small console application for demonstrating this approach and name the project as EFModelFirst.
I am using VS 2012 and with EF 5.0.0 which can be downloaded from nugget using the command in your package manager console as shown below.
Now we are ready to add our models to our solution. For this I will be creating two models named
1. Usergroup
2. User
which will store usergroup and user information respectively. The relation will be one to many ( a usergroup will have multiple users, where as an user will be a member of a single group only).
So select the EDMX file from Project -> Add New Item dialog box and name it as Model.edmx and click on Add.
From the next form select Empty model, because we are trying to create a database from a model and click on Finish
Now the solution explorer will list some additional files that are created by the designer. It will create a EDMX file which will be designer, a DataContext as shown below
It’s now time for adding the models, which can be done from the EDMX designer. Select the Entity option from Toolbox and add the properties of your model as shown below.
Now let’s add the relation between these models using the Association dialog. For that right click anywhere on the designer to bring up the Add Association dialog box and enter the details as shown below.
In this dialog box we will give a name for our association and specify our one-to-many relationship and which all columns need to be mapped. After adding this designer will look like as shown below.
As you can see from the image the now in the designer there are two notification properties that are based on our relation. One in the Usergroup will be a collection, because an usergroup will have more than one user and while an user will be a member of one group only.
For easy reference, I have renamed the EntitySet Name to table name for easy reference and you can skip this one if don’t need to do like that.
Also I have renamed the container name as EFModelFirstContainer.
So that’s all we need to create the models and now we can move ahead and create the database and tables. For that right click on the designer to bring up and context menu and select “Generate Database from Model” option and you will get a dialog box as shown below.
In the next window, it will create all the sql needed for creating the same and will save as file.
You can either execute this from VS or from SQL Server management studio for creating the database.
Now we have everything ready at database level, let’s write some code for inserting the data. In the program.cs file, we will add a group and then add an user to the group.
static void Main(string[] args)
{
using (var db = new EFModelFirstContainer())
{
try
{
var group = db.MT_USERGROUP.Create();
var user = db.MT_USER.Create();
group.GroupCode = "ADMIN";
group.GroupDesc = "Admin Group";
//db.MT_USERGROUP.Add(group);
user.FirstName = "Amal";
user.LastName = "Dev";
user.Username = "amal";
group.Users.Add(user);
db.MT_USERGROUP.Add(group);
db.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
}
Console.ReadLine();
}
}
Here we had created a variable for the container and also for our models.
At first we add the data to our model and then add that model to our container. After that we will call the SaveChanges() method of the container to persist the data to the database.
No Comments
Connecting Azure Blob Storage account using Managed Identity
Posted 12/9/2022Securing Azure KeyVault connections using Managed Identity
Posted 11/26/2022Manage application settings with Azure KeyVault
Posted 11/9/2022Adding Serilog to Azure Functions created using .NET 5
Posted 4/3/2021Learn how to split log data into different tables using Serilog in ASP.NET Core
Posted 4/23/2020