0

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?

7
  • 1. It's not quite clear what you are asking about 2. Remove any requests for documentation/tutorials/sample code.... just ask what you want to know / How to do what you want. Commented Jan 29, 2020 at 2:53
  • @KeithNicholas I made some edit. Thank you for pointing out. Commented Jan 29, 2020 at 2:55
  • you probably want stackoverflow.com/questions/41705235/… Commented Jan 29, 2020 at 2:55
  • 1
    If you don't want to use the data migration feature, then don't use it. You don't have to. I've never used it. Commented Jan 29, 2020 at 2:57
  • 1
    The question is still very unclear. Expose one table, database first, migrations. Commented Jan 29, 2020 at 2:59

2 Answers 2

0

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
Sign up to request clarification or add additional context in comments.

18 Comments

You just can remove all unwanted entity classes .cs and remove all unwanted tables from DbContext.cs if you want to use only 1 table.
DbContext will connect to your table. Eg, your table is UserLogin 1) Add this in your api function => var db = new YourDBContext(); var s = db.UserLogin; 2) Put breakpoint at code line after "var s = db.UserLogin" 3) Call api function through Postman or URL and see if your "s" has list or not (when your UserLogin table has data)
I have done sample document for you. Can check docs.google.com/document/d/… to understand better.
Ok. hope everything fine soon.
Glad to hear that.
|
0

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.

3 Comments

Will this create a multiple models for different tables automatically? I only need a single table.
Ill give it a try. Thanks Gabriel.
You can pick which tables to create classes for.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.