Creating a .NET Core web api that has a route access to an existing Database

.net api c# entity-framework-core

Question

Currently I am reading some documentations on creating web API using the Database first approach. I noticed that most of the tutorials migrate their table on their application. How do I make an web api that only expose or has one route to expose the table in a specific database using Entity Framework that no need data migration?

1
0
1/29/2020 2:55:00 AM

Accepted Answer

You just need to download 4 from nugets and run Scaffold-DbContext command. Download these 4 from nugets.

1) EntityFrameworkCore

2) EntityFrameworkCore.Design

3) EntityFrameworkCore.Tools

4) EntityFrameworkCore.SqlServer

Open Tools > NuGet Package Manager > Package Manager Console. And enter this below in console.

Scaffold-DbContext "Server=yourserveraddress;Database=yourdatabase;user id=youruser;password=yourpassword;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
0
1/29/2020 5:38:17 AM

Popular Answer

So I assume you are trying to create all the classes based on your existing database.

This is done with the Scaffold-DbContext command. You can use it manually as described here, but I prefer to use the EFCorePowerTools plugin. Use the Reverse Engineer feature, which will generate and run the appropriate Scaffold-DbContext command for you.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow