Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Workflows: Model First5:50 with James Churchill
Now let's take a look at the Model First workflow, which reverses the process of defining your model and connecting to a database.
With the Model First workflow the second workflow that EF supported, 0:00 the process of defining your model and connecting to a database is reversed. 0:03 You start with an empty Model and use the EF Designer within 0:08 Visual Studio to define the entities and the relationships between them. 0:12 As with the database workflow the entity and 0:17 context classes are generated from the model. 0:20 But unlike that workflow, the database is also generated from the model. 0:23 The model can be changed during development but 0:28 the database will be dropped and 0:31 recreated when flowing the changes from the application to the database. 0:33 Because of this, once an app goes into production the model becomes 0:37 need only from the application side. 0:42 There are workarounds to this limitation, but 0:44 they require some extra effort to implement. 0:47 See the teachers notes for more information. 0:50 Here's an example model in the EF designer. 0:53 When using the Model First workflow, 0:56 you add an ADO.NET entity data model item to your project and 0:58 select the empty EF Designer model option, in the first step of the wizard. 1:03 Then, you use a combination of the toolbox, and properties windows, 1:08 to create entities, and entity associations. 1:14 You can also add new items, 1:18 by right clicking in an empty space on the EF Designer, and selecting, Add New. 1:20 Our model contains contact and contact email entities 1:26 as well as an association or relationship between the two entities. 1:32 We'll be taking a closer look at how to define or 1:37 create relationships in a later section. 1:40 Once we have our model like we wanted, we can generate the database from our model. 1:44 Right-click on EF Designer and select Generate Database from Model. 1:48 Here's the data connection that we created in the previous video. 1:55 Let's create a new connection in order to change the database name. 1:59 I'll enter the server name from my localbb database server, 2:05 (localdb)\MSSQLLocalDB, and the name for 2:13 the new database, ModelFIrstTestDB. 2:20 And click OK. 2:26 We'll be prompted if we want to create the database, 2:30 which is what we want to do, so I'll answer Yes. 2:35 In the last step, the wizard will show us a preview of the DDL, or 2:39 data definition language, that was generated from our model. 2:44 This DDL is a set of SQL commands that can be used to create our database. 2:48 Click Finish to close the Wizard. 2:55 We need to execute our DDL script in order to create our database tables. 2:58 Click the Execute icon here on the left of this toolbar and 3:04 connect to the database by selecting the server and the database. 3:08 In the message window, we can see that the commands completed successfully. 3:19 Notice that after we generated our database, 3:30 we now have two co-generation templates. 3:33 One to generate the entity classes and another to generate the context class. 3:36 When we save our model, Visual Studio will generate our entity and context classes. 3:43 Here's our contact and contact email entities and here's our context class. 3:51 When making changes to the model, it's possible to edit the EDMX file directly, 4:02 instead of using the visual designer. 4:07 Right click on the EDMX file, and select, Open With. 4:10 Then, select XML (Text) Editor. 4:14 When editing the EDMX file directly, 4:18 we can see that the file is organized into three parts. 4:21 The storage models, the conceptual models, and 4:26 the mappings between the storage and conceptual models. 4:32 When using the database first or model first workflows, modifying or extending 4:38 the generated entity in contents classes means customizing the code generation 4:44 templates that are used to generate these classes or creating partial classes. 4:49 Partial classes are defined using the partial keyword 5:01 right here before the class keyword. 5:04 This allows you to provide the definition of the class across one or 5:07 more physical files. 5:12 That way, one file can be the file that EF generates and 5:13 another can be a file that we add additional properties and methods to. 5:17 So while you can customize the generated in a DE in context classes, 5:23 developers generally just want to work directly with the code 5:27 instead of fiddling around with the visual designer. 5:30 Also, when using the Model First workflow, 5:33 keeping your model in sync with your database can be a tedious process. 5:36 The code first workflow was created to resolve these issues. 5:40 After the break, we'll introduce the code first workflow and 5:45 add our first entity to our project. 5:48
You need to sign up for Treehouse in order to download course files.Sign up