1 00:00:00,570 --> 00:00:03,960 With the Model First workflow the second workflow that EF supported, 2 00:00:03,960 --> 00:00:08,870 the process of defining your model and connecting to a database is reversed. 3 00:00:08,870 --> 00:00:12,575 You start with an empty Model and use the EF Designer within 4 00:00:12,575 --> 00:00:17,352 Visual Studio to define the entities and the relationships between them. 5 00:00:17,352 --> 00:00:20,224 As with the database workflow the entity and 6 00:00:20,224 --> 00:00:23,337 context classes are generated from the model. 7 00:00:23,337 --> 00:00:28,580 But unlike that workflow, the database is also generated from the model. 8 00:00:28,580 --> 00:00:31,381 The model can be changed during development but 9 00:00:31,381 --> 00:00:33,271 the database will be dropped and 10 00:00:33,271 --> 00:00:37,696 recreated when flowing the changes from the application to the database. 11 00:00:37,696 --> 00:00:42,229 Because of this, once an app goes into production the model becomes 12 00:00:42,229 --> 00:00:44,781 need only from the application side. 13 00:00:44,781 --> 00:00:47,333 There are workarounds to this limitation, but 14 00:00:47,333 --> 00:00:50,390 they require some extra effort to implement. 15 00:00:50,390 --> 00:00:52,360 See the teachers notes for more information. 16 00:00:53,730 --> 00:00:56,640 Here's an example model in the EF designer. 17 00:00:56,640 --> 00:00:58,660 When using the Model First workflow, 18 00:00:58,660 --> 00:01:03,190 you add an ADO.NET entity data model item to your project and 19 00:01:03,190 --> 00:01:08,280 select the empty EF Designer model option, in the first step of the wizard. 20 00:01:08,280 --> 00:01:14,140 Then, you use a combination of the toolbox, and properties windows, 21 00:01:14,140 --> 00:01:18,210 to create entities, and entity associations. 22 00:01:18,210 --> 00:01:20,010 You can also add new items, 23 00:01:20,010 --> 00:01:25,050 by right clicking in an empty space on the EF Designer, and selecting, Add New. 24 00:01:26,410 --> 00:01:32,260 Our model contains contact and contact email entities 25 00:01:32,260 --> 00:01:37,785 as well as an association or relationship between the two entities. 26 00:01:37,785 --> 00:01:40,650 We'll be taking a closer look at how to define or 27 00:01:40,650 --> 00:01:42,930 create relationships in a later section. 28 00:01:44,160 --> 00:01:48,145 Once we have our model like we wanted, we can generate the database from our model. 29 00:01:48,145 --> 00:01:53,780 Right-click on EF Designer and select Generate Database from Model. 30 00:01:55,610 --> 00:01:59,620 Here's the data connection that we created in the previous video. 31 00:01:59,620 --> 00:02:05,182 Let's create a new connection in order to change the database name. 32 00:02:05,182 --> 00:02:13,845 I'll enter the server name from my localbb database server, 33 00:02:13,845 --> 00:02:20,511 (localdb)\MSSQLLocalDB, and the name for 34 00:02:20,511 --> 00:02:26,186 the new database, ModelFIrstTestDB. 35 00:02:26,186 --> 00:02:30,670 And click OK. 36 00:02:30,670 --> 00:02:35,474 We'll be prompted if we want to create the database, 37 00:02:35,474 --> 00:02:39,862 which is what we want to do, so I'll answer Yes. 38 00:02:39,862 --> 00:02:44,248 In the last step, the wizard will show us a preview of the DDL, or 39 00:02:44,248 --> 00:02:48,655 data definition language, that was generated from our model. 40 00:02:48,655 --> 00:02:53,730 This DDL is a set of SQL commands that can be used to create our database. 41 00:02:55,620 --> 00:02:57,260 Click Finish to close the Wizard. 42 00:02:58,780 --> 00:03:04,040 We need to execute our DDL script in order to create our database tables. 43 00:03:04,040 --> 00:03:08,264 Click the Execute icon here on the left of this toolbar and 44 00:03:08,264 --> 00:03:13,196 connect to the database by selecting the server and the database. 45 00:03:19,801 --> 00:03:24,446 In the message window, we can see that the commands completed successfully. 46 00:03:30,861 --> 00:03:33,552 Notice that after we generated our database, 47 00:03:33,552 --> 00:03:36,640 we now have two co-generation templates. 48 00:03:36,640 --> 00:03:41,310 One to generate the entity classes and another to generate the context class. 49 00:03:43,290 --> 00:03:48,990 When we save our model, Visual Studio will generate our entity and context classes. 50 00:03:51,030 --> 00:03:58,048 Here's our contact and contact email entities and here's our context class. 51 00:04:02,205 --> 00:04:07,035 When making changes to the model, it's possible to edit the EDMX file directly, 52 00:04:07,035 --> 00:04:10,100 instead of using the visual designer. 53 00:04:10,100 --> 00:04:14,650 Right click on the EDMX file, and select, Open With. 54 00:04:14,650 --> 00:04:17,630 Then, select XML (Text) Editor. 55 00:04:18,750 --> 00:04:21,300 When editing the EDMX file directly, 56 00:04:21,300 --> 00:04:24,750 we can see that the file is organized into three parts. 57 00:04:26,410 --> 00:04:32,240 The storage models, the conceptual models, and 58 00:04:32,240 --> 00:04:35,790 the mappings between the storage and conceptual models. 59 00:04:38,680 --> 00:04:44,094 When using the database first or model first workflows, modifying or extending 60 00:04:44,094 --> 00:04:49,510 the generated entity in contents classes means customizing the code generation 61 00:04:49,510 --> 00:04:54,944 templates that are used to generate these classes or creating partial classes. 62 00:05:01,280 --> 00:05:04,835 Partial classes are defined using the partial keyword 63 00:05:04,835 --> 00:05:07,315 right here before the class keyword. 64 00:05:07,315 --> 00:05:12,193 This allows you to provide the definition of the class across one or 65 00:05:12,193 --> 00:05:13,855 more physical files. 66 00:05:13,855 --> 00:05:17,275 That way, one file can be the file that EF generates and 67 00:05:17,275 --> 00:05:21,840 another can be a file that we add additional properties and methods to. 68 00:05:23,130 --> 00:05:27,150 So while you can customize the generated in a DE in context classes, 69 00:05:27,150 --> 00:05:30,450 developers generally just want to work directly with the code 70 00:05:30,450 --> 00:05:33,350 instead of fiddling around with the visual designer. 71 00:05:33,350 --> 00:05:36,190 Also, when using the Model First workflow, 72 00:05:36,190 --> 00:05:40,650 keeping your model in sync with your database can be a tedious process. 73 00:05:40,650 --> 00:05:45,230 The code first workflow was created to resolve these issues. 74 00:05:45,230 --> 00:05:48,450 After the break, we'll introduce the code first workflow and 75 00:05:48,450 --> 00:05:50,300 add our first entity to our project.