Skip to main content

Using Postgres on Docker with EF7 and ASP.NET 5

In an effort to get a better understanding of the ASP.NET framework, I've started a simple toy project from scratch.  I've really been enjoying how easy it is to plug everything together with Microsoft's reboot of the framework.  Just spin up a project.json file and get going, it's pretty impressive how much you can get done with so few files.
I think the community has been doing a great job with blog posts, so I've not had many opportunities to contribute any unique content that isn't already covered.

While working through things today however, I thought it might be nice to try and prove out how Entity Framework 7 can work with non-Microsoft databases and containers.  The most appealing at the time of writing being postgres thanks to the awesome work by the people over at npgsql.
Because I've been developing in Windows using Visual Studio, the first thing that would be nice is to not have to spoil my system with a database installation.  No surprise, docker seems like it would make a great ally for that!

So, for this blog post, I'd like to take you through setting up a docker container running postgres and then adding support to your ASP.NET 5 project to run migrations against it.

Docker & Database Setup

It's easy to get started with Docker Toolbox, just download and run the installer.  One snag I encountered was that I had Hyper-V enabled on my machine.  This resulted in the docker quickstart terminal giving me some errors about virtualization not being enabled on my machine.
Under the hood, docker toolbox uses VirtualBox which cannot run with Hyper-V.  It's not much at the moment, but I have taken the time to ask that they make docker machine's Hyper-V support a little more user friendly.  I encourage you to head over there and give it a +1 if you agree.

With toolbox downloaded, create a file called local-development.yml at the root of your project.  Take a copy of this code which defines a container configuration based on the official postgres docker image.

If you're running on OSX or Windows and are like me, you'll probably also want the server to behave as though it was on localhost.  You can run this to forward all traffic destined for 5432 on your machine to the docker vm:

docker-machine stop default
VBoxManage modifyvm "default" --natpf1 "app,tcp,,5432,,5432"
docker-machine start default

(Note: VBoxManage may not be on your path in Windows, but it is in the program's installation folder in Program Files.)

Finally, you can try starting the database server by simply typing the following in a docker-enabled terminal at the root of your project:

docker-compose -f local-development.yml up

After which point, you should be able to use any postgres client to connect to the database.  I suggest DataGrip - formerly 0xdbe - made by the folks over at JetBrains.

Project Setup

So far, you've got a database, but now what steps need to be taken to get ASP.NET talking to the containerized database?

Really, things couldn't be any simpler.  First off, you'll want to open up project.json and ensure you have the lines from this gist present.

Make sure you get a package restore done automatically via your tooling or manually by running dnu restore.

Next, you're going to want to make sure you've enabled EF7 in your project's Startup.cs file.  Here's a copy of the method and what you have to call to get that done.  Mix in what I have with what you've got as needed.

The last step, which I've left to this point deliberately is to add the configuration for postgres to your DbContext.
This file is usually going to be heavily tailored to your project.  So I'm going to try and avoid anything to do with your schema.  The file I've shared with you is just barebones enough to have identity and a crude configuration string.  Once you've mixed in my example code with your working DbContext, you should have everything you need to try generating a migration and running it.

Migrating

When I started learning about frameworks, running my first migrations was an oddly rewarding experience.  For me, it completed the circle of understanding and represented the moment that I felt I was able to apply what I had learned to create real solutions.

My hope with this blog post is that you'll feel the same about ASP.NET today as I do.  These next steps are easy.  All you need is a terminal with dnx working at the root of your project:

dnx ef migrations add Initial
dnx ef database update

Which, in my case, got me a new file in my project, as well as this:


And that's it!  You now have access to all the functionality of EF7 and ASP.NET 5.

Keep in mind that while ASP.NET 5 is now RC, there's always some remote chance that something could change.  The one thing I can easily predict now is that any use of dnx will be changing to dotnet.
Beyond that though, this should be a fairly safe blog post.  Most of it concentrates on getting you going with a postgres container.

Final Thoughts

There are a few more things when it comes to containerization that I'd like to get working, mainly adding an entry in local-development.yml for my ASP.NET app.  Although simulating a live environment locally is something that can easily swallow days of effort.  It's usually enough to say that ASP.NET takes care of a lot of environment abstraction for you.
Over time, I feel like if I need to containerize the app itself, I'll just make that a deployment detail and not concern my development workflow with it.
We'll see though, as using containers for actual development can sometimes be quite useful when onboarding new team members.

Hopefully I haven't accidentally left anything out, but if you have any issues getting to a point where you've got your migration generated, let me know in a comment!

Popular posts from this blog

Making TypeScript npm Packages

If you've landed here, I can only assume you're like me and see packages as the highest form of sophistication in software development.  In that same vein, I bet at some point in the past you've wished you could start applying DRY principles to your client-side efforts.  I know for myself, I don't enjoy writing the same application bootstrap code constantly and so recently, I was motivated to codify it.

This body of understanding has taken me quite some while to figure out, hopefully what I share here is helpful enough to get you up to speed.  No post is complete without some kind of example, so throughout I'm going to reference a package I've just finished putting together called protoculture.

Briefly described, protoculture encapsulates all the common bootstrap and conventions I've been using while developing TypeScript apps that use React and Redux.  Honestly, I've already gotten a lot of benefit out of putting this package together, but nothing about…

The Cons, Winnipeg's New Splash Pads

I never had the chance to be ungrateful, the new splash pad at Kildare and Wabasha isn't a splash pad at all.  It was taken away from us and replaced with an "aquatic park", a little bit of wordsmithing designed to gloss over the fact that an open piece of our community has been replaced with yet another closed gate.

As I write this post now, I can hear it already: "Taken away, what?! It's a new water park, you're so..."
Sure, some might reach for that tired recrimination, which is why I started this blog post by dismissing such a premise.  To be fair however, I offer the response: Don't spoil this discussion with nonsense.
You see, I was grateful before the renovations happened.  The communal service on offer was adequate and I never complained about it or saw it as flawed.  Don't believe me? Here, this is a cute google-generated animation of my son enjoying the splash pad in 2014.

Today we took the kids to see if we could spend some time at th…

Building .NET Core Nuget Packages

My last blog post was on building and publishing npm packages, specifically for typescript projects. In my opinion, packages are an important fundamental unit in software development. They sit at the confluence of technical competence and collaboration, and represent something the software community should be proud of.
Most of the time, you're going to be creating software packages once you're comfortable with some essential pillars: CodingProject structureSoftware architectureBuildingDeliveryCommunityLicensing
After I got my npm package up and running, my next task was to do the same thing with my C# libraries. Similar to protoculture, I have made a library called praxis.  This is done by leveraging the services and tooling known in the .NET ecosystem as nuget.


In this case, praxis abstracts many of the concepts and functionality I require when producing server projects. It builds on top of ASP.NET Core, so in that sense you can almost think of it as a framework-framework. The…