I am working on my first web api and I went through this tutorial and followed it for the most part. However, the web api I need to build has to connect to an existing sql server database. Does anybody have an example of connecting a web api to an existing database which is created and maintained from an outside application in MVC6?
Do web APIs use dbcontexts the same as MVC websites do? How do you add an existing database in configure services in startup?
If you add EF in configureservices won't EF try to scaffold the DB using code first in my model?
Update: After the comments from Oleg and the answer below I discovered that web api does indeed connect to the database the same so going through the tutorial on mvc 6 adding an existing database helped, plus the comment Oleg made.
The key items I was missing were the following:
- You do need to add a dbContext, I didn't scaffold it.
You can use entity framework in your startup configureServices.
services.AddEntityFramework() .AddSqlServer() .AddDbContext(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
Is it valid to have an empty DBContext like so?
public class ApiDbContext : DbContext
{
}
It seems to run and return a query result so I am guessing it is but is it better to scaffold it out with EF?