|
| 1 | +Entity Framework (EF) Core s a .NET library that provides an object-relational mapping (ORM) framework for .NET developers to work with databases. It supports multiple database providers, including SQL Server, MySQL, PostgreSQL, SQLite, and others. |
| 2 | + |
| 3 | +## Getting started |
| 4 | + |
| 5 | +### Prerequisites |
| 6 | + |
| 7 | +Make sure to install the same version of all EF Core packages shipped by Microsoft. For example, if version 5.0.3 of Microsoft.EntityFrameworkCore.SqlServer is installed, then all other Microsoft.EntityFrameworkCore.* packages must also be at 5.0.3. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +To use Microsoft.EntityFrameworkCore in your application, you will typically need to create a class that inherits from DbContext, which represents your database context. You can then define classes that represent your database entities, and use LINQ queries to interact with the database. |
| 12 | + |
| 13 | +Here's an example of how you might define a database context and an entity: |
| 14 | + |
| 15 | +```c# |
| 16 | +using Microsoft.EntityFrameworkCore; |
| 17 | + |
| 18 | +public class MyDbContext : DbContext |
| 19 | +{ |
| 20 | + public DbSet<Customer> Customers { get; set; } |
| 21 | +} |
| 22 | + |
| 23 | +public class Customer |
| 24 | +{ |
| 25 | + public int Id { get; set; } |
| 26 | + public string Name { get; set; } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +You can then use the MyDbContext class to interact with the database: |
| 31 | + |
| 32 | +```c# |
| 33 | +using (var context = new MyDbContext()) |
| 34 | +{ |
| 35 | + // Add a new customer |
| 36 | + context.Customers.Add(new Customer { Name = "John Doe" }); |
| 37 | + context.SaveChanges(); |
| 38 | + |
| 39 | + // Retrieve all customers |
| 40 | + var customers = context.Customers.ToList(); |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +Microsoft.EntityFrameworkCore supports multiple database providers, including SQL Server, MySQL, PostgreSQL, SQLite, and others. You will need to install the provider package for your chosen database. For example, to use SQL Server, you would install the [Microsoft.EntityFrameworkCore.SqlServer package](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer/7.0.3). |
| 45 | + |
| 46 | +You would then configure your database context to use the SQL Server provider: |
| 47 | + |
| 48 | +```c# |
| 49 | +using Microsoft.EntityFrameworkCore; |
| 50 | + |
| 51 | +public class MyDbContext : DbContext |
| 52 | +{ |
| 53 | + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
| 54 | + { |
| 55 | + optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyDatabase;"); |
| 56 | + } |
| 57 | + |
| 58 | + public DbSet<Customer> Customers { get; set; } |
| 59 | +} |
| 60 | + |
| 61 | +public class Customer |
| 62 | +{ |
| 63 | + public int Id { get; set; } |
| 64 | + public string Name { get; set; } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +## Additional documentation |
| 69 | + |
| 70 | +- [Getting Started with Entity Framework Core](https://learn.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli). |
| 71 | +- Follow the [ASP.NET Core Tutorial](https://learn.microsoft.com/en-us/aspnet/core/data/ef-rp/intro?view=aspnetcore-7.0&tabs=visual-studio) to use EF Core in a web app. |
| 72 | +- [Releases and planning(roadmap)](https://learn.microsoft.com/en-us/ef/core/what-is-new/) |
| 73 | +- [How to write an EF Core provider](https://learn.microsoft.com/en-us/ef/core/providers/writing-a-provider) |
| 74 | + |
| 75 | +## Feedback |
| 76 | + |
| 77 | +If you have a specific question about using these projects, we encourage you to ask it on [Stack Overflow](https://stackoverflow.com/questions/tagged/entity-framework-core+or+entity-framework-core-2.1+or+entity-framework-core-3.1). If you encounter a bug or would like to request a feature, [submit an Github issue](https://github.com/dotnet/efcore/issues/new/choose). For more details, see [getting support](https://github.com/dotnet/efcore/blob/main/.github/SUPPORT.md). |
0 commit comments