4

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:

  1. You do need to add a dbContext, I didn't scaffold it.
  2. 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?

Xaxum
  • 3,545
  • 9
  • 46
  • 66
  • 1
    You can take a look in [the answer](http://stackoverflow.com/a/34764698/315935) and [this one](http://stackoverflow.com/a/34393532/315935). Both shows the usage of `dynamic` (as `ExpandoObject`) for reading from the database. The style of codding don't contains any direct definition of fields in C# code and you can use, for example, stored procedures defined in the database. No scaffolding is required in the style. If you do prefer scaffolding then you can do this too. See [the answer](http://stackoverflow.com/a/34457974/315935) with description of the parameters of `dnx ef dbcontext scaffold` – Oleg Feb 14 '16 at 15:55
  • See [the tutorial](http://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.html) too. – Oleg Feb 14 '16 at 15:58
  • @Oleg Many thanks for the additional resources. It looks like the examples you have given are MVC web apps which I have done a few of. I guess the idea behind the new web api is they are the same in .net 5 so hopefully that works. Anyway thanks for the time in finding these resources. I will post my results if I am successful in connecting the web api to my DB. – Xaxum Feb 14 '16 at 16:05

1 Answers1

3

Do web APIs use dbcontexts the same as MVC websites do?

In ASP.NET Core there is no longer a differentiation between “Web API” and “MVC”. Both are handled by MVC controllers and the only difference between an API and a website is that a website returns a View() call, whereas the API returns something else (e.g. a Json() call, or even just a plain object which is then automatically serialized).

How do you add an existing database in configure services in startup?

In order to use existing databases, you still need to setup a database context for it. It’s just that instead of creating the database with EF, you just have it there. You can use the dnx ef dbcontext scaffold command to scaffold a database context and model types from an existing database (you need to pass in the connection string there as well), or you can start with an empty database context and just create it manually from scratch.

But for using EF, it doesn’t really matter if the database was created by EF or not. You need to have the correct models and mapping set up, and then it’s just the decision whether to use the EF commands to create the database or not. You do get the support for database migrations then too (which you can use in a way with existing databases), but you still don’t have to use them if you don’t need them.

If you add EF in configureservices won't EF try to scaffold the DB using code first in my model?

If you remove the Database.Migrate() call (in the Startup.Configure method) that exists in some standard project templates for ASP.NET Core, then EF will not do anything. You have complete control over where and when to execute scaffolding or migrations.

poke
  • 369,085
  • 72
  • 557
  • 602