Asp.net 5 MVC 6 web api access to existing database

I am working on my first web avi and I went through this tutorial and followed it for the most part. However, the web api I need to create must connect to an existing sql server database. Does anyone have an example of connecting a web api to an existing database that is created and maintained from an external application in MVC6?

Do the dbcontexts web interfaces use the same as MVC websites? How do you add an existing database to configuration services at startup?

If you add EF to configureservices, will EF not try to fake the database using code first in my model?

Update: After Oleg’s comments and the answer below, I found that the web api really connects to the database in the same way as through the mvc 6 tutorial, adding the existing database that was added, as well as the comment made by Oleg.

The key elements that I was missing were the following:

  • You need to add dbContext, I have not faked it.
  • You can use the entity infrastructure in your startup configuration services.

    services.AddEntityFramework () .AddSqlServer () .AddDBContext (options => options.UseSqlServer (Configuration ["Data: DefaultConnection: ConnectionString"]));

Is it permissible to have empty DBC text?

public class ApiDbContext : DbContext
{
}

, , , , - EF?

+4
1

- dbcontexts , - MVC?

ASP.NET Core "Web API" "MVC". MVC, API - , - View(), API - (, Json() , ).

?

, . , EF, . dnx ef dbcontext scaffold ( ), .

EF , EF . , , EF . ( ), , .

EF configureservices, EF DB, ?

Database.Migrate() ( Startup.Configure), ASP.NET, EF . , .

+3

Source: https://habr.com/ru/post/1628721/


All Articles