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.
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.
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.
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.
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!
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
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!
Comments
Post a Comment