What are Migrations?4:41 with James Churchill
Let's start with a look at the challenge of maintaining our data model after our application has been released into production and how Code First Migrations helps us with propagating data model changes to the database. We'll also review what you'll learn in this course.
To get the most from this course, you should feel comfortable with C#, Visual Studio, and the basics of Entity Framework. For more information about those topics, check out these Treehouse courses and workshops:
While it's not absolutely required, you'll also find it helpful to know something about relational databases and the SQL language. Check out this Treehouse course:
For more information about the EF Code First workflow or Code First Migrations, see the following MSDN (Microsoft Developer Network) resources.
[MUSIC] 0:00 Hi, my name is James and I'm a developer. 0:04 In an earlier Treehouse course we saw how Microsoft's Entity Framework allows us to 0:08 leverage the power of databases within our own applications by writing C# code. 0:12 This is so helpful for developers. 0:18 Instead of interacting directly with a database. 0:20 We interact with the set of entities and a database context class all in C#. 0:23 We see this pattern across languages and technologies, Entity Framework 0:30 which is commonly referred to simply as EF wraps around the database and 0:34 maps everything in it to objects that we can easily using code. 0:39 This type of framework or library is known as an object relational mapper or ORM. 0:44 EF's code first workflow allows us to define and 0:49 manipulate the data persistence in our applications in an easy and 0:53 natural way as we write the C# code for the app itself. 0:57 Once we've defined our model by creating entity in context classes, 1:02 EF will automatically generate the database at runtime. 1:06 When we make changes to our model, EF can be configured to automatically drop and 1:10 create the database so that it keeps in sync with our model. 1:15 This approach works great as long as we don't have actual user data in our system. 1:19 Let's look at an example. 1:25 We work with our team to develop, test and 1:28 deploy version 1.0 of our application and to production. 1:31 And our app is a huge hit with our users. 1:34 Our users love our app so much that they start asking for new features and 1:38 enhancements. 1:43 We wanna keep our users happy, so we start working on implementing their requests. 1:44 To do that, we need to update our model. 1:49 But doing so will result in the database being dropped and 1:51 created, which would result in data loss. 1:55 That's not going to work, our users rely upon the data that 1:59 they have persisted in our applications database. 2:02 We need a way to keep our database in sync with our model 2:05 without resulting in any user data loss. 2:09 What are our options? 2:14 We could use SQL to write database alter scripts that would 2:16 update our database schema. 2:20 Then we could manually update our projects entity classes 2:22 to keep them in sync with the database. 2:25 Earlier in my career I worked on a project that use this approach 2:27 we spent a lot of time and effort keeping our model up to date with 2:32 the changes that were being made to the database. 2:36 Worst of all, sometimes we weren't even aware of the changes that were being made 2:38 to the database until our application simply went work. 2:42 Alternatively we could use Visual Studio to generate our entity and 2:47 context classes. 2:51 But we wouldn't want to lose any of the read only properties or 2:53 methods that we've added to our entity classes. 2:56 So we'd need to manually merge the changes from the generated entity classes 2:59 into our project's entity classes. 3:04 Again, this would work, but 3:06 it still requires too much effort, and is still prone to errors. 3:09 Luckily there is a better way. 3:14 EF code first migrations allows you to update an existing database 3:16 when your model changes without losing any data. 3:21 Even better, in keeping with the spirit of the code first workflow, 3:24 migrations are expressed using code. 3:28 After making a change to our model, we can use EF to generate a migration. 3:31 Each migration is represented by a C# class that is added to our project. 3:36 Since the migration is just a class, 3:41 we can review it before we apply it to our database. 3:43 This gives us an opportunity to catch mistakes in our model. 3:47 Furthermore, because each migration is represented by its own class 3:51 our project will naturally include a history of our model changes. 3:55 This helps us to understand how our model has changed and evolved over time. 4:00 Let's review what will learn in this course. 4:06 We'll use EF to enable migrations for our project. 4:09 We'll add migrations, both an initial migration and 4:13 a migration after a model change, and see how to update our database. 4:16 We'll also look at an example of how to modify a migration. 4:22 And we'll review the various deployment options available to us. 4:25 By the end of this course, you'll be able to leverage code first migrations 4:29 to keep your database in sync with your model. 4:34 Let's start by seeing how to enable migrations. 4:38
You need to sign up for Treehouse in order to download course files.Sign up