Entity Framework - Create Table using C# classes using Code First approach
Entity Framework - Create Table Using C# Classes with the Code First Approach
Entity Framework (EF) is a popular Object-Relational Mapping (ORM) framework for .NET applications. It allows developers to interact with a database using C# classes, making data manipulation more intuitive and less prone to errors. In this blog post, we will explore how to create a table in a database using the Code First approach in Entity Framework, following the principles discussed in the YouTube video "Entity Framework - Create Table using C# classes using Code First approach."
What is Code First Approach?
The Code First approach in Entity Framework allows developers to define their database schema using C# classes. This means you can start by writing your classes and then use EF to create the corresponding database tables automatically. It is particularly useful when you have an existing domain model or want to keep your database schema in sync with your code.
Setting Up Your Project
To get started, ensure you have the following prerequisites:
- Visual Studio: Download and install the latest version of Visual Studio if you haven't already.
- Entity Framework: Ensure you have added Entity Framework to your project. You can do this via NuGet Package Manager.
To install Entity Framework, run the following command in the NuGet Package Manager Console:
Install-Package EntityFramework
Creating Your Model Classes
Step 1: Define Your Entities
Let's create a simple example where we define a Product class. This class will represent a table in our database.
public class Product
{
public int Id { get; set; } // Primary key
public string Name { get; set; }
public decimal Price { get; set; }
}
Step 2: Create Your DbContext
Next, we need to create a DbContext class. This class will manage the entity objects during runtime and handle the database connection.
using System.Data.Entity;
public class ApplicationDbContext : DbContext
{
public DbSet<Product> Products { get; set; } // DbSet for Product entity
}
Configuring the Database Connection
In order to connect to a database, you need to specify the connection string in your App.config or Web.config file. Here’s an example of a connection string for a local SQL Server database:
<configuration>
<connectionStrings>
<add name="ApplicationDbContext"
connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MyDatabase;Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Creating the Database
Step 1: Enable Migrations
To create and manage changes to the database schema, you'll need to enable migrations. Open the Package Manager Console and run:
Enable-Migrations
This generates a Migrations folder where you can track changes to your database schema.
Step 2: Create a Migration
After enabling migrations, you can create a migration script that corresponds to your model changes. Run the following command:
Add-Migration InitialCreate
This command creates a new migration file in the Migrations folder, which contains the necessary code to create the Products table based on your Product class.
Step 3: Update the Database
Finally, apply the migration to the database with the following command:
Update-Database
This command will execute the migration and create the Products table in your database.
Conclusion
Congratulations! You've successfully created a table in your database using the Code First approach in Entity Framework. By following these steps, you can easily define your domain models in C# and let Entity Framework handle the database interactions. This approach not only streamlines development but also ensures that your database schema remains consistent with your application’s data model.
For further learning, consider experimenting with relationships, data annotations, and querying data using LINQ with Entity Framework. Happy coding!
Connect with SkillBakery Studios
Explore more tutorials, tools, and resources:
Posted by SkillBakery Studios


No comments:
Post a Comment