Skip to content
This repository was archived by the owner on Dec 26, 2019. It is now read-only.

WebAPI & EF

Arjan Einbu edited this page Oct 21, 2015 · 7 revisions

WebAPI & EF

Exercise 1

Create the data model classes (EF code first)

  • Create the entity classes that correspond to the javascript classes
Entity Classes
Place (abstract)
Home (inherits Place)
Business (inherits Place)
Shop (inherits Place)
  • Create the DbContext subclass and expose the Places table
  • Use EF migrations framework to setup the database
    • When we don't specify the connection string, EF will use the following defaults:
Value
server (localdb)\mssqllocaldb
database our-namespace.our-dbcontext-subclass

Create the Place WebAPI controller with methods for the following:

  • Configure WebAPI to use camelCasing and typename handling:
// In App_Start/WebApiConfig.cs
var settings = config.Formatters.JsonFormatter.SerializerSettings;
settings.TypeNameHandling = TypeNameHandling.All;
settings.Formatting = Formatting.Indented;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
Methods Parameters
get all places
get place by id id
add place place
delete id
  • Test in browser by navigating to /api/places

  • Bonus: Test adding a Place with fiddler

    • Insert a row in the database by issuing a POST request to /api/places with Content-Type: application/json and the following payload:
{
    "$type": "OneCircleWeb.DAL.Home, 1CircleWeb",
    "latitude": 59.917290,
    "longitude": 10.727012,
    "title": "Slottet",
    "whoLivesHere": "Harald and Sonja"
}

Clone this wiki locally