Database First Approach
Introduction to Database First Approach in C# Entity Framework
The Database First Approach in Entity Framework is a technique where the database is designed first, and then the application is developed around it. In other words, you create your database, tables, and relationships first, and then you generate an Entity Data Model (EDM) from the existing database.
This approach is advantageous when you already have a database in place with your desired schema, and you want to build an application using this existing database.
Setting Up
Before you begin, ensure you've installed the necessary software:
- Visual Studio
- SQL Server
Creating the Database
Let's start by creating a database. Open your SQL Server Management Studio and create a new database. Name it 'EmployeeDB'. Now, we'll create a table named 'Employee'. This table will have three columns: ID, Name, and Position.
Here's the SQL script to create this table:
CREATE TABLE Employee
(
ID INT PRIMARY KEY,
Name NVARCHAR(50),
Position NVARCHAR(50)
)
After running the script, you should now have a table in your 'EmployeeDB' database.
Creating the Application
Now, let's create a Console Application in Visual Studio.
- Open Visual Studio and click on
File > New > Project
. - Select
Console App (.NET Framework)
and name it 'DatabaseFirstDemo'. - Click on
Create
.
Installing Entity Framework
Before we proceed, we need to install Entity Framework in our project.
Go to Tools > NuGet Package Manager > Manage NuGet Packages for Solution
. In the NuGet tab, click on Browse
and search for Entity Framework
. Click on Install
to add it to your project.
Generating the Model
Now that we have Entity Framework installed, we can generate the model from our existing database.
- Right-click on your project in Solution Explorer and select
Add > New Item
. - In the left panel, select
Data
and then selectADO.NET Entity Data Model
. - Name it 'EmployeeModel' and click on
Add
. - In the Entity Data Model Wizard, select
EF Designer from database
and click onNext
. - Now, click on
New Connection
. In the Server name box, enter your server name, and in the Select or enter a database name box, enter 'EmployeeDB'. Click onOK
. - Click on
Next
. In the Database Objects and Settings page, select the tables you want to include in your model (in this case, the Employee table) and click onFinish
.
You should now see an EDM in your Solution Explorer. This model will have a class named 'Employee' which corresponds to the 'Employee' table in your database.
Using the Model
Now that our model is ready, we can use it in our application. Here's a simple example of how to add a new employee to the 'Employee' table:
using (var context = new EmployeeDBEntities())
{
var employee = new Employee
{
ID = 1,
Name = "John Doe",
Position = "Software Engineer"
};
context.Employees.Add(employee);
context.SaveChanges();
}
Conclusion
That's it! You've successfully created a database, generated a model from it, and used this model in your application. The Database First Approach is a fantastic way to rapidly develop applications when you already have an existing database schema. It saves you the trouble of manually creating and configuring your models, as all of this is done automatically by Entity Framework.
Remember, practice makes perfect. So, keep practicing and exploring more features of Entity Framework. Happy coding!