. The use and finishing of Efcore in Netcore

Source: Internet
Author: User

The Entirtyframework framework is a lightweight, extensible version of the popular Entity Framework data access technology.

One of them. Netcore version corresponds to Entityframeworkcore

Git source code address: https://github.com/aspnet/EntityFramework/

Official Use document Description: Https://docs.microsoft.com/zh-cn/ef/core/index

First, install the NuGet package

install-Packagemicrosoft.entityframeworkcoreinstall-package Microsoft.EntityFrameworkCore.SqlServer
Core package of the Micorsoft.EntityFrameworkCore:EF framework
Micorsoft.EntityFrameworkCore.SqlServer: For SQL Server database extensions, the SQL Server database must be used. Similar to Mysql,sqllite and so on.
Micorsoft.EntityFrameworkCore.Tools
&micorosft.entityframeworkcore.design: The user generates the model code based on the existing database, and more reference: https://docs.microsoft.com/zh-cn/ef/ Efcore-and-ef6/porting/port-edmx
Ii. Examples of Use
1. After installing the NuGet package, manually create the context and inject the SQL link string
usingMicrosoft.entityframeworkcore;namespacecore2{ Public classTestcontext:dbcontext {//Public TestContext (dbcontextoptions<testcontext> options): Base (options)//{        //}        protected Override voidonconfiguring (Dbcontextoptionsbuilder optionsbuilder) {//inject SQL link stringOptionsbuilder.usesqlserver (@"server=.;D Atabase=test1; Trusted_connection=true;"); }         PublicDbset<numeber1> Numeber1s {Get;Set; } }}
2. Handwritten entity classes, as long as there is a corresponding table in the database can be
usingSystem.ComponentModel.DataAnnotations;usingSystem.ComponentModel.DataAnnotations.Schema;namespacecore2{[Table ("Numeber1")]     Public classNumeber1 {[Key] Public intID {Get;Set; }  Public decimalNUM1 {Get;Set; } }}

3. Test the code:

Static voidTestone () {TestContext _context=NewTestContext (); intCount =_context.    Numeber1s.count (); Console.WriteLine (count);}Static voidTesttwo () {DateTime start=DateTime.Now; TestContext _context=NewTestContext ();  for(inti =0; I <10000; i++) {_context. Numeber1s.add (NewNumeber1 () {Num1=i}); _context.    SaveChanges (); } Console.WriteLine (_context.    Numeber1s.count ()); Console.WriteLine ("total time, number of seconds:"+ (DateTime.Now-start). totalseconds);}

10,000 insert Data Execution time in debug state:


Third, based on the database generation model
1. Installing Entityframeworkcore.design and Entityframeworkcore.tools
2. Select the corresponding project, perform the generation naming
" server=.; Database=test1; Trusted_connection=true; " Microsoft.entityframeworkcore.sqlserver-outputdir MODELS2

3. The resulting results are as follows:


More:
VS code build. Netcore Development Environment (II)
VS code build. Netcore Development Environment (i)
Chocolatey Introduction (software Automation management tools)

. The use and finishing of Efcore in Netcore

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.