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...
- Right click "Models" choose "Add" "New Item"
- Choose "Data" : "ADO.NET Entity Data Model". Give it a good name
- Choose "EF Designer from database" then Next

- Choose/Create your Azure data connection. Next.
- Choose the database table you want to expose.
- Click Finish
- You should see a new .edmx file. Make sure it's in your Models folder (move it if nec).
- You should also see in you Server Explorer under "Data Connections" a new .nnnEntities entry.
- 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.
- Rebuild solution.
- Now right click "Controllers"
- Choose "Add" : "Controller"
- Choose Web API 2 ... using Entity Framework
- Choose the model class that was auto created earlier
- Choose the Data Context Class that matches the entity in your data connections.
- Click Add
- Rebuild
- Run in debug mode
- Click API at the top. You should see some documentation about your new stuff!
optional: Add swagger to make it super cool.
- Project: Manage NuGet Packages
- Search for Swashbuckle and install the package name "Swashbuckle"
- Rebuild and run
- add "swagger" to the URL. Something like "http://localhost:53691/swagger"
- Done!
No comments:
Post a Comment