Adding Our First Entity Using the Code First Workflow6:02 with James Churchill
Now let's take a look at the Code First workflow, which allows you to define your model by directly writing the code for your entity and context classes. We'll also add the first entity to our model.
Code First from Existing Database
It's possible to use the Code First workflow with an existing database, by generating your entity and context classes from your database. Generally speaking, this is a one-time operation. Future updates to your entity classes are typically done manually.
For more information on this process, see this tutorial:
The last of the three workflows to be added to EF, 0:00 the code first workflow takes yet another point of view by allowing you to 0:02 define your model by directly writing the code for your entity and context classes. 0:07 At runtime, EF will generate our entity and 0:11 context classes and in memory entity data model. 0:15 The code first workflow allows you to define and 0:19 manipulate the data persistence for your applications in a very natural way, 0:22 as you write the C# code for the app itself. 0:27 Once you've defined your model, you can then automatically generate the database 0:31 at run time or you can use a feature called Code First Migrations to generate 0:35 the database and keep it in sync with your model as it evolves and changes over time. 0:40 We'll cover Code First Migrations in a later course. 0:45 The code first workflow is a natural fit for a new application development. 0:49 With that being said, if you have an existing database you can generate your 0:54 entity and context classes from your database as a one time operation. 0:58 See in the teacher's notes for more information. 1:03 In contrast to the database or model first workflows, which can sometimes be tedious 1:06 to use, developers often find the code first workflow to be fun to use. 1:11 In this course, we'll be using EF within the context of a console application. 1:17 This will give us an easy way to test and 1:22 debug our EF code without having to write a lot of unrelated code. 1:24 Then, in a later course, 1:29 we'll see how to integrate entity framework with an ASP.NET NVC app. 1:31 Let's set up our project in Visual Studio and 1:37 get started with using the code first workflow to define our model. 1:39 >> Go ahead and open the new project dialog and 1:44 select the Console Application project template. 1:47 >> I'll name the project ComicBookGalleryModel and update 1:53 the location to be a folder named src within the repo that I created earlier. 1:58 Now we can use the NuGet package manager to add EF to our project. 2:15 We don't even need to search for the package. 2:22 It's the second most popular package at the time of this recording 2:25 with 22.1 million downloads. 2:28 To install the package, click the install button, then OK, and 2:33 accept the license In the Output window, we can monitor the installation progress. 2:38 And it's finished now. 2:46 Let's add our entity class, but first let's add a folder named 2:51 Models This folder will contain all of our project's entity classes. 2:56 Right click on the folder, and select, Add > Class. 3:06 Name the class, ComicBook and click Add. 3:12 I'll go ahead and make the class public. 3:22 There's no compelling reason at this time to restrict access to this class. 3:26 And I'll add the following properties, 3:31 an int property named ID. 3:36 This property will contain a unique value for each ComicBook in our system. 3:39 A string property named SeriesTitle. 3:46 Each ComicBook belongs to a series. 3:52 This property is the title of the series that the ComicBook belongs to. 3:55 An int property named IssueNumber. 4:01 Within the series that a comic book belongs to, 4:06 each issue has a unique number. 4:09 Issue one, issue two, issue three, and so on. 4:11 A string property named Description. 4:17 This property is the description for the comic book. 4:24 And a DateTime property named PublishedOn. 4:28 This property is the date that the ComicBook was published on. 4:36 The code's first workflow uses a set of conventions in order to know how 4:40 to interpret your entity classes and 4:45 generate the entity data model that the EF will use. 4:47 For instance, the entity class name will become the table name, either 4:51 the singular or plural version of the name depending on how EF is configured. 4:56 And if the class contains a property named Id, a primary key column with the same 5:02 name and data type will be added to the entity's database table. 5:07 Because where using a numeric data type, 5:11 EF will also make the column an identity column. 5:14 Identity columns are automatically assigned values by the database 5:17 when new records are added to the table. 5:22 Primary key properties, can also be named ID, 5:27 using upper case letters, the class name, followed by Id, or the class name, 5:31 followed by ID, using upper case letters. 5:35 Any of these options will work fine. 5:44 Whichever one you or your team decides to use just be consistent. 5:46 Now that we've seen how to create an entity let's do a code challenge so 5:52 you can practice creating your own. 5:56 After that we'll add the context class to our project. 5:58
You need to sign up for Treehouse in order to download course files.Sign up