Entity Framework Core in Action, Second Edition by Jon P Smith;
Author:Jon P Smith; [Smith, Jon P]
Language: eng
Format: epub
Publisher: Simon & Schuster
Published: 2021-06-15T22:00:00+00:00
A more complex but more versatile approach is to extend the MigrationBuilder class to add your own commands. This approach allows you to access methods to build commands that work for many database providers.
In this section, I discuss only the first approach. The second is an advanced version that is well described in the EF Core documentation at http://mng.bz/xGBe.
As an example, you are going to create an extension method that will allow you to create SQL Views more easily. The extension method takes in the class that will be mapped to the View so that it can find the properties to map to the columns (assuming that you are using only properties and By Convention column naming). The following listing shows the extension method that will create a view within a migration.
Listing 9.6 Extension method to add/alter an SQL view in an EF Core migration
public static class AddViewExtensions ⶠ{ public static void AddViewViaSql<TView>( â· this MigrationBuilder migrationBuilder, ⸠string viewName, â¹ string tableName, â¹ string whereSql) ⺠where TView : class â» { if (!migrationBuilder.IsSqlServer()) â¼ throw new NotImplementedException(âwarning...â) â¼ var selectNamesString = string.Join(", ", â½ typeof(TView).GetProperties() â½ .Select(x => x.Name)); â½ var viewSql = $"CREATE OR ALTER VIEW {viewName} AS " + â¾ $"SELECT {selectNamesString} FROM {tableName} " + â¾ $"WHERE {whereSql}"; â¾ migrationBuilder.Sql(viewSql); â¿ } }
ⶠAn extension method must be in a static class.
â· The method needs the class that is mapped to the view so that it can get its properties.
⸠The MigrationBuilder provides access to the migration methodsâin this case, the Sql method.
â¹ The method needs the name to use for the view and the name of the table it is selecting from.
⺠Views have a Where clause that filters the results returned.
â» Ensures that the TView type is a class
â¼ This method throws an exception if the database isnât Server because it uses an SQL Server view format.
â½ Gets the names of the properties in the class mapped to the view and uses them as column names
â¾ Creates the SQL command to create/update a view
â¿ Uses MigrationBuilderâs method to apply the created SQL to the database
You would use this technique in a migration by adding it to the Up method (and a DROP VIEW command in the Down method to remove it). Here is a code snippet that creates a view for the MyView class, which has the properties MyString and MyDateTime:
migrationBuilder.AddViewViaSql<MyView>( "EntityFilterView", "Entities", "MyDateTime >= '2020-1-1'");
The resulting SQL looks like this snippet:
CREATE OR ALTER VIEW EntityFilterView AS SELECT MyString, MyDateTime FROM Entities WHERE MyDateTime >= '2020-1-1'
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7415)
Grails in Action by Glen Smith Peter Ledbrook(7311)
Kotlin in Action by Dmitry Jemerov(4662)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4141)
The Age of Surveillance Capitalism by Shoshana Zuboff(3435)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3260)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3035)
Mastering Python for Networking and Security by José Manuel Ortega(2971)
Blockchain Basics by Daniel Drescher(2902)
Microsoft 365 Identity and Services Exam Guide MS-100 by Aaron Guilmette(2738)
TCP IP by Todd Lammle(2649)
Configuring Windows Server Hybrid Advanced Services Exam Ref AZ-801 by Chris Gill(2595)
From CIA to APT: An Introduction to Cyber Security by Edward G. Amoroso & Matthew E. Amoroso(2490)
Azure Containers Explained by Wesley Haakman & Richard Hooper(2484)
Hands-On Azure for Developers by Kamil Mrzyglod(2436)
React Native - Building Mobile Apps with JavaScript by Novick Vladimir(2346)
The Social Psychology of Inequality by Unknown(2322)
MCSA Windows Server 2016 Study Guide: Exam 70-740 by William Panek(2320)
The Art Of Deception by Kevin Mitnick(2308)