WebAPI with ninject and code first — Part 1

Kelvin Kok
Feb 23, 2017 · 1 min read

Quick blog on WebAPI on .NET framework with correct approach.

There are many blogs and tutorials talk about how to do WebAPI and dependency injection, but the key problem for these tutorials and blogs do not give real world implementation example.

However, you might ask my code functionality works well, why do I need to follow these best practice or coding pattern. The answer to that is if you are building a small project, and yes this might work well. However, if you are working on project a few hundred thousand lines then you need proper structure to ensure your code is maintainable. And this is called Software Engineering.

Entity framework code first approach

This enables you to create your database structure using class files. In old days you need to create these classes manual or using tools. Entity framework helps you to automate the process. When you have database changes, just trigger migration command and you will get the delta generated in .cs file. And you can trigger update database to push the database changes to your database.

How to do it ?

  1. Use nuget to add ‘EntityFramework’ in your package.
  2. Type Add-migration “init” in package manager console, this will create migration class file. If you cannot see package manager console, go to [View] → [Other windows] → [Package Manager Console] to activate it
  3. Type Update-database to push the changes to database, database structure will be updated based on the migration class file generated

Next, we will talk about dependency injection.

Kelvin Kok

Written by

Cloud architect with passion in emerging technologies and digital transformation. The posts are my personal opinion on technology.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade