How many times have we all worked on some technology and then wanted to use it again on a different project but couldn’t figure out how to get started? We’ve all done it, and Entity Framework with Code First is definitely one of those technologies that is hard to get started.
…unless you have a checklist. That’s why I’m writing this. It’s my checklist. It helps me start new projects and hopefully it helps you, too. I’m going to assume for the purpose of this post that you’re somewhat familiar with EF and a high level overview will suffice. There will be more posts later that will take the various steps into greater detail, and I’ll link to them from here as I get them written. So, without further adieu…
Create a Model
Add a folder named “Models” to your project and place one or more models in it. A model is just a simple POCO. Make sure it has a field named “Id.” For getting started, I like to use just one, simple model. That way, if something isn’t working later, I have less variables to deal with. Here’s some tips for creating solid models.
Add the Entity Framework Packages
Entity Framework doesn’t come already loaded in projects, so you have to add it. The best way to get the correct, latest package is to do it from the Package Manager console. Just go in, make sure the correct project is selected, and type “Install-Package EntityFramework” and you’re done.
Extend DbContext
You’ll need a Context for dealing with all of your entities. Unlike database first, you need to create it yourself, which is easier than it sounds. Create a folder in your project to isolate this boilerplate stuff. I like to use DAL, for Data Access Layer. You can name it Fred, I don’t care. In that folder, create a class called SomethingSomethingContext (where SomethingSomething is something that makes sense in your project). The only thing you have to do now is create a collection property for your model. If your model was Customer, then you would need this line of code:
public DbSet<Customer> Customers { get; set; }
Add a Connection String
Remember that connection string in database first? The really long one that pointed back to code and made it hard to deploy? Forget that. The connection string for code first is a simple connection string that is really easy to replace in a deployment. The only rule is that it has to have the same name as the context class you just made. You can override that behavior, but why make this harder than it needs to be.
Enable Code Migrations
Open the Package Manager Console again, and enter the command Enable-Migrations. This creates a Migrations folder with a migration configuration class. The defaults for that are fine.
Add Your First Migration
While still in Package Manager, enter the command “Add-Migration ‘InitialDb'” to create your initial migration file. It should contain a command for creating a table based on the model you created earlier. Check it out and make sure it’s doing what you want. My most common mistake that I find here is forgetting to make value type fields Nullable (e.g. int?), so that they can be null in the table.
Update Your Database
If the migration looks good, run the command “update-database” to create the database with your table in it. The most likely thing to go wrong here is a bad connection string, check that before stomping around in your code.
Start Using It
Now you’re all ready to go. Hopefully, it worked out and your code is working. You can see a super simple example of a project started this way here.