Entity Framework (EF) is an open-source Object-Relational Mapping (ORM) framework for the Microsoft .NET framework. It is designed to work with Microsoft SQL Server and is used to interact with databases in a more object-oriented way, rather than using the traditional relational approach.
EF allows developers to work with a conceptual model of the data, rather than working directly with the database, making it easier to create, read, update, and delete data. It also provides a way to map the conceptual model to the underlying database schema, allowing for a more efficient and streamlined data access experience.
Entity Framework Integration
Integrating C# with databases using Entity Framework involves several steps:
- Create a new project in Visual Studio and add the Entity Framework NuGet package to it.
- Create a conceptual model of your data using classes and properties. These classes will be mapped to the database tables and properties will be mapped to the columns.
- Create a database context class that will act as a bridge between the conceptual model and the database. This class should inherit from the DbContext class provided by EF.
- Use the context class to interact with the database. EF provides a set of methods such as
Add
,Update
,Remove
andSaveChanges
that can be used to perform CRUD operations on the data. - Use LINQ to query the data using the context class.
- Use a connection string to connect to the database.
- Optionally, use migrations to create and update the database schema.
using System; using System.Linq; using Microsoft.EntityFrameworkCore; //1. Create a new project in Visual Studio and add the Entity Framework NuGet package to it. //2. Create a class for the data model public class Person { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } } //3. Create a database context class public class MyContext : DbContext { public DbSet<Person> People { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=mydb;Trusted_Connection=True;"); } } //4. Use the context class to interact with the database class Program { static void Main() { using (var context = new MyContext()) { // Add a new person to the database var person = new Person { Name = "John Doe", Age = 30 }; context.People.Add(person); context.SaveChanges(); // Query the database var people = context.People.Where(p => p.Age > 25).ToList(); // Update a person var firstPerson = context.People.First(); firstPerson.Name = "Jane Doe"; context.SaveChanges(); // Delete a person context.People.Remove(firstPerson); context.SaveChanges(); } } }
Please note that for this code to work, you need to have SQL Server installed on your machine, this is just an example the connection string may change depending on the server and the database you are connecting to.
This is a simple example but it demonstrates the basic operations, you can use the context class to perform more complex queries and procedures, and it also provides a way to handle database relationships, such as one-to-many, many-to-many, and many-to-one, in a more natural way.
Once these steps are completed, you can use the context class to interact with the database in a more object-oriented way, rather than writing raw SQL queries.
Entity Framework Core Components
Entity Framework Core (EF Core) has several key components that work together to provide an easy and efficient way to interact with databases:
- DbContext: The DbContext class is the main class used to interact with the database. It acts as a bridge between the conceptual model and the database and provides a set of methods for performing CRUD operations.
- DbSet: The DbSet class represents a set of entities in the database. Each DbSet property in the context class corresponds to a table in the database.
- Migrations: EF Core provides a way to create and update the database schema using migrations. This allows you to update the database schema as your application evolves without losing data.
- Model: The model represents the conceptual view of the data. It is composed of classes and properties that correspond to the tables and columns in the database. EF Core uses the model to map the data to the database schema.
- LINQ: EF Core allows you to use LINQ to query the data. This provides a more natural and efficient way to query the data, rather than writing raw SQL queries.
- Providers: EF Core provides a set of providers that enable it to work with different types of databases, such as SQL Server, MySQL, and PostgreSQL.
- DataAnnotations: EF core uses data annotations to map the model to the database schema. It provides several attributes that can be used to configure the mapping between the model and the database.
- Fluent API: EF Core also provides a fluent API for configuring the mapping between the model and the database. It allows for more advanced configuration options than data annotations.
These components work together to provide a flexible and efficient way to interact with databases in C#. EF Core allows you to work with a conceptual model of the data, rather than working directly with the database, making it easier to create, read, update, and delete data.