Entity Framework Core ‐ Code First - ChrispyPeaches/FocusFriends GitHub Wiki
Entity Framework Core (EFC) is an approach for structuring a database (DB). All models are created via C# code, and then relational mapping/table generation is autogenerated by EFC. Relations can also be manually adjusted via C# code as well.
The FocusApp's database is automatically created, but we'll need to create the FocusAPI's database.
After a DB is setup and ready to be populated with tables, the NuGet packages that were used to implement Entity Framework Core in the FocusAPI are:
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore.Design
- Microsoft.EntityFrameworkCore.Tools
After installing the tools, create two folders in your project called 'Data' and 'Models'. This is where you will store the Models(tables) in Models and Database Context in Data.
- If necessary, change the connection string, otherwise one is preconfigured
- Open the Package Manager Console
- Run the following command
Update-Database -Project FocusAPI -StartupProject FocusAPI
A model in EFC acts as a template for table generation. They're stored in a project's Models folder.
(Images taken from Microsoft EFC Docs: https://learn.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli)
It consists of multiple attributes which will become columns, and can imply relationships. In this example, a Primary Key is autogenerated from BlogId and it understands the implicit relationships between other objects, such as Post.
Within the data folder is the database context. Here, the mappings are created and manual mappings can be made. Using the previous model Blog, our mapping would look like: public DbSet<Blog> Blogs { get; set; }
This is also where the connection to the database is established. An example connection with the DB connection string directly injected would look like:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Your DB Connection String Here");
}
Note that it is poor practice to store a direct string here. A better option would be appsettings.json.
After the models are created and the DB connection is set, the DB is ready to be populated with tables. This is a similar process to git commits. To do this, simply open your package manager console found in tools via Visual Studio. Find this in the top toolbar under Tools > NuGet Package Manager > Package Manager Console.
A "Migrations" Folder will also be autogenerated to store these and snapshots of the DB. Once you double-check everything in the migration was generated correctly, type Update-Database
in the package manager console, and the database should be generated with all relations and keys ready.
- Open the Package Manager Console
- Run the following command
- For the FocusAPI:
Add-Migration "<migration name>" -Project FocusAPI -StartupProject FocusAPI
- For the FocusApp
Add-Migration "<migration name>" -Context FocusApp.Shared.Data.FocusAppContext -Project FocusApp.Shared -StartupProject FocusApp.Tools
- For the FocusAPI:
- Verify the migration was generated correctly by applying all migrations to a database and reviewing the schema
- For the FocusApp, this can be done by
- Open the Package Manager Console
- Run the following command
- For the FocusAPI
Update-Database -Project FocusAPI -StartupProject FocusAPI
- For the FocusApp, this is done automatically on startup, but if you want to view the schema, run this command, then review the schema of the database generated in the FocusApp.Tools folder
Update-Database -Context FocusApp.Shared.Data.FocusAppContext -Project FocusApp.Shared -StartupProject FocusApp.Tools
- For the FocusAPI