Monthly Archives: March 2016

Angular2 Http Authentication Interceptor

In my Angular2 application I want to be redirected to the login page whenever I get a 401 response during an Ajax call. Therefore I want to intercept all Ajax calls and check for the response code. In addition to that I also want to set a couple of default request headers for each Ajax call. The code below shows an interceptor for angular2 http requests.

Continue reading

ASP .NET 5 Identity with Entity Framework 7 Setup

In this article we will demonstrate how to provide authentication to ASP .NET 5 applications using ASP .NET Identity with Entity Framework 7.

Adding Packages

Add the following package to your ASP .NET project. This will automatically pull in additional required dependencies for ASP .NET Identity.

project.json

Continue reading

Using Lombok 1.16 with Netbeans 8.1 and Maven

Project Lombok provides annotations which reduce boilerplate code in Java applications (see Listing 1). The Netbeans IDE supports it almost out-of-the-box. You just have to enable the option “Project Properties > Build > Compiling > Enable Annotation Processing”. However the current version 1.16.8 is not working using Netbeans 8.1 and Maven.

Continue reading

Dependency Injection in ASP .NET 5 MVC 6

With ASP .NET 5 MVC 6 you do not have to rely on external Dependency Injection (DI) libraries any more. It comes with it’s on onboard DI. I will now demonstrate how to set up Dependency Injection for using Entity Framework 7 DbContexts within Controllers.

Register Service

For simplicity of this example I will not create a separate interface for my DbContext or implement a repository pattern for accessing the database. In bigger projects, I strongly recommend to do so, however. If you want to keep it simple like me you can directly register the DbContext for later on injection into Controllers in the class Startup.cs.

Continue reading

Code First Migrations Entity Framework 7

Create Initial Code First Migration

In order to do Code First Migrations, you first have to set up the command dnx ef. Code first means, that you first do database model changes in the Entity Framework database classes and then apply the changes to the actual database. Please read the 2 previous blog entries about ASP .NET 5 on how to set up the dnx command line tool.

To be more specific, the examples in this article were done using Entity Framework 7 RC1. If you execute dnx ef in your ASP .NET project folder, you will get the following output.

Continue reading

Entity Framework 7 Scaffold DbContext from Existing Database

In previous to 7 versions of the Entity Framework all Entity Framework related commands could be executed within Visual Studio. Now you have to use the new ASP .NET 5 dnx command instead.

DbContext Model Migration from Entity Framework 6

In order to migrate my model from Entity Framework 6 to Entity Framework 7, I did not want to manually adjust my old DbContext for Entity Framework 7. Instead I used scaffolding from my existing database to create a completely new model. This minimizes the risk of introducing errors in the new model. After I generated the model, I deleted the previous Migration History table in the database. This might not be suitable for everyone, but I wanted to make a clean cut after transitioning to EF 7.

Scaffold DbContext

In order to scaffold a DBContext from an existing database, you first have to set up dnx ef. If you have not done so already, please read the 2 previous blog entries about ASP .NET 5.

To be more specific, the examples in this article were done using Entity Framework 7 RC1. If you execute dnx ef in your ASP .NET project folder, you will get the following output.

Continue reading