The script required for creating the Post table and inserting the data is displayed below:
// Code removed for brevity CREATETABLE [dbo].[Post]( [Id] INTIDENTITY (1, 1)NOTNULL, [BlogId] INTNOTNULL, [Content] NVARCHAR (MAX)NULL, [PublishedDateTime] DATETIME2 (7)NOTNULL, [Title] NVARCHAR (MAX)NOTNULL ); GO // Code removed for brevity INSERTINTO [Post]([BlogId], [Title], [Content], [PublishedDateTime])VALUES (1,'Dotnet 4.7 Released','Dotnet 4.7 Released Contents', '20170424'), (2,'.NET Core 1.1 Released','.NET Core 1.1 Released Contents', '20170424'), (2,'EF Core 1.1 Released','EF Core 1.1 Released Contents', '20170424') GO
While inserting data time values, we should use the YYYYMMDD format, such as 20170424, and if we are tuned with the DD-MM-YYYY hh:mm:ss xm format, then we need to perform an explicit conversion, such as convert(datetime,'24-04-2017 01:34:09 PM',5), otherwise we would get the message, the conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
We need to figure out how to execute the previous script using LocalDB:
Open the SQL Server Object Explorer from the View menu.
Expand SQL Server and (localdb)\MSSQLLocalDB.
Right-click on Databases and select Add New Database.
In the Create Database dialog box, provide the Database Name as MasteringEFCoreDbFirst and Database Location as your project path, and click OK.
Expand Databases, right-click on the MasteringEFCoreDbFirst database, and select New Query.
Copy the Blog.sql content and paste it into the New Query window, and click the execute icon or Ctrl+Shift+E:
The script execution of the Blog script is shown as follows:
Execute Blog.sql on the New Query window
Copy the Post.sql content and paste it in the New Query window, and click the execute icon or Ctrl+Shift+E:
The script execution of the Post script is shown here:
Execute Post.sql on New Query window
We have prepared the database for the Database-First approach; now let's see how we could leverage Entity Framework on the existing database.