Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Overriding the Context's OnModelCreating Method6:33 with James Churchill
Let's override the Context class' `OnModelCreating` method so that we can customize EF's conventions and use EF's fluent API to refine our model.
To follow along commiting your changes to this course, you'll need to fork the dotnet-comic-book-gallery-model repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork> cd dotnet-comic-book-gallery-model git checkout tags/v3.7 -b overriding-the-contexts-onmodelcreating-method
Data annotation attributes aren't the only tool that EF provides to refine 0:00 our data model. 0:05 We could also override the on model creating method in the context class so 0:06 that we can customize ES conventions or use EF fluent API to refine our model. 0:11 If we type the override keyword, 0:17 Visual Studio will display a list of the base class methods that we can override. 0:19 Remember, overridable methods are methods in the base class that 0:26 are marked with a virtual keyword. 0:30 By overriding a method will be placing or 0:32 extending the behavior of the base class method. 0:34 The base keyword here gives us a way to call the base classes version of 0:38 this method. 0:42 In this case, the base class is OnModelCreating method doesn't have 0:43 an implementation, so it's not important to call the base class method. 0:47 By not calling the base class method our code 0:52 will replace the base classes implementation. 0:55 One of the things that we can do within the on model creating method is to add or 0:58 remove items in the collection of conventions 1:03 that EF uses when creating our model. 1:06 One of those conventions is the pluralizing table name convention. 1:09 If we remove this convention, EF will use our entity class names as is. 1:13 So in our case, our table names will be singular, as our entity class names are. 1:19 To remove the pluralizing table name convention, 1:24 we need to call the remove method on the modelBuilder's Conventions property. 1:27 modelBuilder.Conventions.Remove. 1:32 The Remove method is available as a generic method, 1:38 which allows us to pass in the convention type that we want to remove, 1:41 which in our case is pluralizing table name convention. 1:45 We'll need to add a using directive for the system, data, entity, 1:56 model configuration, conventions, namespace. 2:00 This convention is just one of EF's many conventions. 2:04 For a complete list see the teacher's notes. 2:08 We can also use ES fluent API, to control every aspect of our model. 2:11 In fact, there are some things that can only be done by using the fluent API. 2:16 Remember a comic book average rating property? 2:21 Here's our ComicBooks table. 2:27 Notice that the average rating columns data type is decimal(18,2). 2:29 The first number in parenthesis, 18, is the precision of the column, 2:34 and the second number, two, is the the scale. 2:39 Precision is the number of digits in a number. 2:43 Scale is the number of digits to the right of the decimal point in the number. 2:46 For example, the number 123.45 has a precision of 5 and a scale of 2. 2:50 The AverageRating column has a precision of 18 and a scale of 2. 2:58 That allows for a number as big as one quadrillion point zero zero. 3:03 That's a pretty big number for an average rating. 3:09 Let's look at a couple of ways that we can change the average rating columns 3:13 data type. 3:17 One option would be to use the decimal property convention class 3:18 to change the convention that EF uses when generating our model. 3:23 First, you need to remove the existing convention 3:27 modelBuilder.Conventions.Remove(DecimalPr- opertyConvention>(); 3:30 Then you can add the convention back by adding a new instance of 3:39 the DecimalPropertyConvention class. 3:43 The constructor of this class allows us to specify what the default precision and 3:45 scale should be. 3:50 ModelBuilder.Conventions.Add(new DecimalPropertyConvention), 3:51 and we'll pass in 5 for the precision and 2 for the scale. 4:00 While this approach would work, 4:05 it would change the data type for every decimal column in our database. 4:07 We currently only have one decimal column. 4:11 So, technically for our situation, this isn't a problem, though 4:14 using this approach might have unexpected consequences in the future if or 4:18 when we added another decimal property to our model. 4:23 By using the fluent API, we can be more precise with our model refinement. 4:26 First, we need to get a reference to the ComicBook entity in our model by making 4:32 a call to the modelBuilder's entity method. 4:36 modelBuilder entity<comicbook< div=""></comicbook<>. 4:39 Once we have a reference to our entity we can then use the property method 4:45 to get access to the specific property that we want to update. 4:49 .Property( cb goes to cb.AverageRating). 4:53 On the property object we can use the has precision method to set the precision and 4:59 scale of the property. 5:04 HasPrecision, five for the precision, and two for the scale. 5:07 Let's test our model changes. 5:13 Our console output still looks correct. 5:17 If you already have the SQL Server Object Explorer window open, 5:29 refresh the list of tables. 5:32 Notice that all of our table names are no longer pluralized, but 5:36 now match our singular entity class names. 5:40 And our ComicBook tables average rating columns data type is now decimal five two. 5:48 We're just scratching the surface of the fluent API, for 5:56 instance it's possible to rely completely upon the fluent API. 5:59 To refine your model, and not use any data and 6:04 annotation attributes in your indie classes. 6:06 If that's your or your development teams preference, or 6:09 you can use the fluent API to define relationships. 6:13 Using this method to define and many to many relationship gives you control over 6:17 the implicit bridge table name or the column names within the bridge table. 6:21 If you'd like to learn more about how to use the flu in API, 6:27 see the teacher's notes for links to additional resources. 6:30
You need to sign up for Treehouse in order to download course files.Sign up