Monday, August 8, 2016
In this post will are going to finally create the database that we have been preparing for in the last previous blog posts. It's a two step process, first you have to add the NorthwindContext to the application in the Startup class, then you have to run the Entity Framework migration tool.
Here are the steps to create your NorthwindCafe database:
1. Open the Startup.cs file, then type the following lines in the ConfigureServices method
The line above gets the connection string from the appSettings.json file that we've created earlier. Then use the AddDbContext method in the services instance. Dependency injection will take care of the plumbing for you. Using lamba expression we tell the Entity Framework to use the Sql Sever provider for Entity Framework core.
Make sure you have the following namespaces in your Startup class
using NorthwindCafe.Web.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
2. Now we can run the dotnet ef tools in the command line, first right-click on the NorthwindCafe.Web project and select "Open Command Line" → Default
3. In the command line type dotnet ef migrations add InitialDatabase your screen should look like below
4. Now you have to run another command to actually create the database type dotnet ef database update to create the NorthwindCafe database in SQL Server
The first command dotnet ef migrations add InitialDatabase creates two files in the "Migrations" folder in the NorthwindCafe.Web project. These files contains the codes for creating the database and any other subsequent migrations that you will run in the future. Everytime you run a migration a snapshot is created with the NorthwindContextModelSnapshot
The second dotnet ef database update command uses the migration script in Visual Studio to update the database. After you run this command you will see that the NorthwindCafe database has been created. The amazing thing is that the foreign key constraint has been added for us, because we used the virtual keyword to define our property in our models in this post.
Here are the steps to create your NorthwindCafe database:
1. Open the Startup.cs file, then type the following lines in the ConfigureServices method
var connectionString = Configuration["Data:NorthwindContextConnection"]; services.AddDbContext<NorthwindContext>(options => options.UseSqlServer(connectionString));
The line above gets the connection string from the appSettings.json file that we've created earlier. Then use the AddDbContext method in the services instance. Dependency injection will take care of the plumbing for you. Using lamba expression we tell the Entity Framework to use the Sql Sever provider for Entity Framework core.
Make sure you have the following namespaces in your Startup class
using NorthwindCafe.Web.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
2. Now we can run the dotnet ef tools in the command line, first right-click on the NorthwindCafe.Web project and select "Open Command Line" → Default
3. In the command line type dotnet ef migrations add InitialDatabase your screen should look like below
4. Now you have to run another command to actually create the database type dotnet ef database update to create the NorthwindCafe database in SQL Server
The first command dotnet ef migrations add InitialDatabase creates two files in the "Migrations" folder in the NorthwindCafe.Web project. These files contains the codes for creating the database and any other subsequent migrations that you will run in the future. Everytime you run a migration a snapshot is created with the NorthwindContextModelSnapshot
The second dotnet ef database update command uses the migration script in Visual Studio to update the database. After you run this command you will see that the NorthwindCafe database has been created. The amazing thing is that the foreign key constraint has been added for us, because we used the virtual keyword to define our property in our models in this post.
ASP.NET Core Posts:
- How To Create An ASP.NET Core Application From Scratch
- ASP.NET Core : Add jQuery, Bootstrap, AngularJS Using bower.json
- Enable MVC On ASP.NET Core Application
- ASP.NET Core : Create Our First Controller and View
- ASP.NET Core : Adding The Default View With _ViewStart.cshtml
- ASP.NET Core : Create A Responsive Layout With Bootstrap
- ASP.NET Core : Adding Font-Awesome For Northwind Cafe Navigation Icons
- ASP.NET Core : Add .json Configuration Files With Microsoft.Extensions.Configuration Library
- ASP.NET Core : Entity Framework Core Models For Northwind Cafe
- ASP.NET Core : Create The NothwindContext ( EntityFrameworkCore )
- ASP.NET Core : Configure project.json File To Support Entity Framework Core
- ASP.NET Core : Add NorthwindContext To Startup Class And Create Database
- ASP.NET Core: Seeding The NorwindCafe Database
- ASP.NET Core: Add Logging To The NorthwindCafe Application
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
Thank you for great tutorial. Good to know how to make a project from scratch, thanks to it I can see how it exactly work.
ReplyDeleteLooking forward to next updates :).
Aivivu đại lý vé máy bay, tham khảo
ReplyDeletesăn vé máy bay giá rẻ đi Mỹ
thông tin chuyến bay từ mỹ về việt nam
các đường bay từ canada về việt nam
bay nhật bản việt nam
giá vé máy bay từ hàn quốc về việt nam
Vé máy bay từ Đài Loan về VN
chuyen bay danh cho chuyen gia