The ASP. NET Core Series "II" uses the entity Framework core for additional pruning checks

Source: Internet
Author: User
Tags new set

Original: ASP. NET Core series "two" use the entity Framework Core for additional pruning and checking

Introduction Entityframeworkcore

EF Core is a lightweight, extensible, cross-platform version of EF. EF Core has many promotions and new features for EF, and EF Core is a brand new codebase that is not as mature and stable as EF6.
EF Core maintains a similar development experience with EF, and most top-level APIs are preserved, so if you've ever used EF6, then you'll find it very easy and familiar to get started with EF core, which is built on a whole new set of core components, which means EF The core does not inherit all the attributes from the EF. Some features will be released in future releases (such as lazy loading and elastic linking), and some of the other less common features will not be implemented in EF core.
New, extensible, lightweight EF core that allows us to add some features that are not implemented in EF6 (for example, alternate keys, and a mixed compute Hybrid database in LINQ)

Install and configure

Download EF Core using the NuGet package management tool

Send text Microsoft.EntityFrameworkCore.SqlServer Convenient small partners copy,

When you install this, you will automatically install the required dependencies, and you can use the Efcore when you are finished.

1. Create a new model as test data

2. Create a new UITL folder to hold EF-related classes,

New context

3. Inject the context

The ASP. NET core implements DI by default, and the service is registered at startup and obtained through constructors.

Open Startup.cs, register the context

4 Modifying the supporting DefaultConnection

Open Appsettings.json File

5. Add test data to build the database structure

Modify the Configure method in 6.Startup, call the method you just

Run the site and you can see that both the database and the test data have been generated

Note: The DbInitializer.cs is for the program to run, build the database, and table structure, etc., manually built data can not be used such operations.

using EF Core

NET core comes with dependency injection by default, so we use EF directly in the controller to inject the constructor

Dependency injection of a bit everyone should have some understanding, do not need to go to new a context, originally:

Privite xxxcontext db=new xxxcontext ();

Right now:

With the breakpoint, we can get the user data.

The ASP. NET Core Series "II" uses the entity Framework core for additional pruning checks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.