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?

Ping
  • 41
  • 6
  • 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. – Keith Nicholas Jan 29 '20 at 02:53
  • @KeithNicholas I made some edit. Thank you for pointing out. – Ping Jan 29 '20 at 02:55
  • you probably want https://stackoverflow.com/questions/41705235/entity-framework-core-creating-model-from-existing-database – Keith Nicholas Jan 29 '20 at 02: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. – Gabriel Luci Jan 29 '20 at 02:57
  • 1
    The question is still very unclear. Expose one table, database first, migrations. – dropoutcoder Jan 29 '20 at 02:59
  • @GabrielLuci If its okay , may I know how you do it? Most of the tutorials and documentations only do data migration feature. – Ping Jan 29 '20 at 02:59
  • @dropoutcoder Its basically like I want to create a web api using .net core, this api will access the table. The problem is most documentantion for database first migrate the whole data of that table. I dont want to do that since i got thousands of data. I just want to access it for developing a mobile application. – Ping Jan 29 '20 at 03:56

2 Answers2

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.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84
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
Asherguru
  • 1,687
  • 1
  • 5
  • 10
  • I only need one table. I think this will automatically create the other tables in the database. – Ping Jan 29 '20 at 05:43
  • 1
    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. – Asherguru Jan 29 '20 at 06:06
  • It didn't create other tables in the database. It only create entities classes .cs and DbContext in your web api project based on your existing database. – Asherguru Jan 29 '20 at 06:21
  • I am testing it right now. I'll delete the other models created and remove other context. How will I know if it works and was able to connect to the table? – Ping Jan 29 '20 at 06:23
  • 1
    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) – Asherguru Jan 29 '20 at 06:29
  • 1
    I have done sample document for you. Can check https://docs.google.com/document/d/1BVr-yqlTkvvdE8m_ZNrFlHwwsmLkB9OzjzYhqH-LjUI/edit?usp=sharing to understand better. – Asherguru Jan 29 '20 at 06:59
  • I encounter an error while running the app. InvalidOperationException: Unable to resolve service for type 'NETCoreWebApiDemo.DBTestContext' while attempting to activate 'NETCoreWebApiDemo.Controllers.SampleController'. – Ping Jan 29 '20 at 07:01
  • did you google for this error? need to see all your codes. – Asherguru Jan 29 '20 at 07:06
  • I got it now. I add the connectionstring in the ConfigureServices in the startup.cs. Im currently testing. – Ping Jan 29 '20 at 07:12
  • 1
    Ok. hope everything fine soon. – Asherguru Jan 29 '20 at 07:15
  • I encountered an error while debugging. 1. Error = Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation. 2. SyntaxError: JSON.parse: unexpected end of data at line 1 column 2 of the JSON data but when I continue the compiler without debugging I was able to see raw data in my browser. JSON file was still loading but its working now. You are very patient on me. Thank you very much Sir. – Ping Jan 29 '20 at 07:24
  • 1
    Glad to hear that. – Asherguru Jan 29 '20 at 07:27
  • I know this might be old. But do you perhaps have an idea how to apply pagination on this core web api? The api results too many data. Because of that I cant generate the JSON file. – Ping Feb 03 '20 at 08:06
  • You mean show 10 records only with selected page number? context.YourTable.Skip(PageNumber * 10).Take(10); – Asherguru Feb 03 '20 at 08:08
  • Yep. 10 or 50. I cant generate the whole json. Maybe just a simple request query string. – Ping Feb 03 '20 at 08:10
  • You can use this code I typed. – Asherguru Feb 03 '20 at 08:11
  • Where should I add it? In the startup.cs? – Ping Feb 03 '20 at 08:12
  • 1
    in your api function where you get data from database. – Asherguru Feb 03 '20 at 08:23