You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

efcore.md 2.7 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. ---
  2. uid: Guides.OtherLibs.EFCore
  3. title: EFCore
  4. ---
  5. # Entity Framework Core
  6. In this guide we will set up EFCore with a PostgreSQL database. Information on other databases will be at the bottom of this page.
  7. ## Prerequisites
  8. - A simple bot with dependency injection configured
  9. - A running PostgreSQL instance
  10. - [EFCore CLI tools](https://docs.microsoft.com/en-us/ef/core/cli/dotnet#installing-the-tools)
  11. ## Downloading the required packages
  12. You can install the following packages through your IDE or go to the nuget link to grab the dotnet cli command.
  13. |Name|Link|
  14. |--|--|
  15. | `Microsoft.EntityFrameworkCore` | [link](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore) |
  16. | `Npgsql.EntityFrameworkCore.PostgreSQL` | [link](https://www.nuget.org/packages/Npgsql.EntityFrameworkCore.PostgreSQL)|
  17. ## Configuring the DbContext
  18. To use EFCore, you need a DbContext to access everything in your database. The DbContext will look like this. Here is an example entity to show you how you can add more entities yourself later on.
  19. [!code-csharp[DBContext Sample](samples/DbContextSample.cs)]
  20. > [!NOTE]
  21. > To learn more about creating the EFCore model, visit the following [link](https://docs.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli#create-the-model)
  22. ## Adding the DbContext to your Dependency Injection container
  23. To add your newly created DbContext to your Dependency Injection container, simply use the extension method provided by EFCore to add the context to your container. It should look something like this
  24. [!code-csharp[DBContext Dependency Injection](samples/DbContextDepInjection.cs)]
  25. > [!NOTE]
  26. > You can find out how to get your connection string [here](https://www.connectionstrings.com/npgsql/standard/)
  27. ## Migrations
  28. Before you can start using your DbContext, you have to migrate the changes you've made in your code to your actual database.
  29. To learn more about migrations, visit the official Microsoft documentation [here](https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/?tabs=dotnet-core-cli)
  30. ## Using the DbContext
  31. You can now use the DbContext wherever you can inject it. Here's an example on injecting it into an interaction command module.
  32. [!code-csharp[DBContext injected into interaction module](samples/InteractionModuleDISample.cs)]
  33. ## Using a different database provider
  34. Here's a couple of popular database providers for EFCore and links to tutorials on how to set them up. The only thing that usually changes is the provider inside of your `DbContextOptions`
  35. | Provider | Link |
  36. |--|--|
  37. | MySQL | [link](https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework-core-example.html) |
  38. | SQLite | [link](https://docs.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli) |