Thursday, March 3, 2016

Create a REST service SQL Database!

Create a "cloud" database with using LightSwitch like my other posting.

You'll need Visual Studio 2013 ( I use 2015 Community). Connect your server to your Azure db

New Project: ASP.NET Web Application, choose Web API. Choose run in cloud and fill out your Azure stuff. You can set database to no database as we'll add it later.

The magic...

  1. Right click "Models" choose "Add" "New Item"
  2. Choose "Data" : "ADO.NET Entity Data Model". Give it a good name
  3. Choose "EF Designer from database" then Next
  4. Choose/Create your Azure data connection. Next.
  5. Choose the database table you want to expose.
  6. Click Finish
  7. You should see a new .edmx file. Make sure it's in your Models folder (move it if nec).
  8. You should also see in you Server Explorer under "Data Connections" a new .nnnEntities entry.
  9. Open the web.config, you should see 2 data connections. One will be "DefaultConnection" that is pointing locally. Change its value to the other one which should be pointing to azure.
  10. Rebuild solution.
  11. Now right click "Controllers"
  12. Choose "Add" : "Controller"
  13. Choose Web API 2 ... using Entity Framework
  14. Choose the model class that was auto created earlier
  15. Choose the Data Context Class that matches the entity in your data connections.
  16. Click Add
  17. Rebuild
  18. Run in debug mode
  19. Click API at the top. You should see some documentation about your new stuff!
optional: Add swagger to make it super cool.
  1. Project: Manage NuGet Packages
  2. Search for Swashbuckle and install the package name "Swashbuckle"
  3. Rebuild and run
  4. add "swagger" to the URL. Something like "http://localhost:53691/swagger"
  5. Done!

No comments:

Post a Comment