Wednesday, August 21, 2019
In our previous post we created the models for our Northwind Cafe application. In this blog we will create the DbContext class which is the conduit between your entity classes and the database. Think of it as a bridge that the database and the entity framework has to cross to get to each other.
Follow the steps below to create the NorthwindContext:
1. Create a class in Models folder call NorthwindContext
2. Type in the following code in the NorthwindContext class
Make sure you import the namespace Microsoft.EntityFrameworkCore. I will go over the code above line by line.
This means the NorthwindContext is a subclass of the DbContext class
The NorthwindContext constructor tells .NET to pass the DbContextOptions to the base class. We need this because we will add the NorthwindContext in the Startup.cs class in another post.
The DbSet represents the tables and entities that will be created for this context. That's about it, the next few posts will go over how to add the NorthwindContext to our application and create our new Northwind database from scratch.
Follow the steps below to create the NorthwindContext:
1. Create a class in Models folder call NorthwindContext
2. Type in the following code in the NorthwindContext class
using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace NorthwindCafe.Web.Models { public class NorthwindContext : DbContext { public NorthwindContext(DbContextOptions<NorthwindContext> options) :base(options) { } public DbSet<Product> Products { set; get; } public DbSet<Category> Categories { set; get; } public DbSet<Order> Orders { get; set; } public DbSet<OrderDetail> OrderDetails { get; set; } } }
Make sure you import the namespace Microsoft.EntityFrameworkCore. I will go over the code above line by line.
public class NorthwindContext : DbContext
This means the NorthwindContext is a subclass of the DbContext class
public NorthwindContext(DbContextOptionsoptions) :base(options) { }
The NorthwindContext constructor tells .NET to pass the DbContextOptions to the base class. We need this because we will add the NorthwindContext in the Startup.cs class in another post.
public DbSet<Product> Products { set; get; } public DbSet<Category> Categories { set; get; } public DbSet<Order> Orders { get; set; } public DbSet<OrderDetail> OrderDetails { get; set; }
The DbSet represents the tables and entities that will be created for this context. That's about it, the next few posts will go over how to add the NorthwindContext to our application and create our new Northwind database from scratch.
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
Đặt vé máy bay tại Aivivu, tham khảo
ReplyDeleteVé máy bay đi Mỹ
vé máy bay từ new york về việt nam
vé máy bay từ canada về việt nam giá rẻ
vé máy bay từ tokyo vé hà nội
ve may bay vietnam airline tu han quoc ve viet nam
Vé máy bay từ Đài Loan về VN
vé máy bay chuyên gia sang Việt Nam
Hello mate nice postt
ReplyDelete