. NET core uses EF core

Source: Internet
Author: User

First step: Create a. NET Core console app.

Step Two: Install the Efcore package and design (formerly VS is the EF project template, the core version does not now, all install this tool to create modelstype context, etc.).

Tools-->nuget Package Manager--Package Management Console

1.install-package Microsoft.entityframeworkcore.sqlserver

2.install-package microsoft.entityframeworkcore.sqlserver.design

3.install-package Microsoft.entityframeworkcore.tools–pre (-pre should be the preview version)

Step three: Use the Scaffold-dbcontext (database context scaffolding) Directive of the tool to generate the models and context.
NuGet Package Manager Inside: PM

Pm>scaffold-dbcontext "Data source=.;i Nitial catalog=database1; User Id=sa; password=123456 "Microsoft.entityframeworkcore.sqlserver-outputdir Model

Instruction detailed Description:

Scaffold-dbcontext [-connection] <String> [-provider] <String> [-outputdir <string>] [-context < String>]
[-schemas <string>] [-tables <string>] [-dataannotations] [-force] [-project <string>]
[-startupproject <string>] [-environment <string>] [<commonparameters>]

PARAMETERS
-connection <String>
Specifies the connection string of the database.

-provider <String>
Specifies the provider to use. For example, Microsoft.EntityFrameworkCore.SqlServer.

-outputdir <String>
Specifies the directory to use to output the classes. If omitted, the top-level project directory is used.

-context <String>
Specifies the name of the generated DbContext class.

-schemas <String>
Specifies the schemas for which to generate classes.

-tables <String>
Specifies the tables for which to generate classes.

-dataannotations [<switchparameter>]
Use Dataannotation attributes to configure the model where possible. If omitted, the output code would use only the fluent API.

-force [<switchparameter>]
Force scaffolding to overwrite existing files. Otherwise, the code would only proceed if no output files would is overwritten.

-project <String>
Specifies the project to use. If omitted, the default project is used.

-startupproject <String>
Specifies the startup project to use. If omitted, the solution ' s startup Project is used.

-environment <String>
Specifies the environment to use. If omitted, "development" is used.

Examples of this article:

Scaffold-dbcontext "SERVER=192.168.1.159;DATABASE=SGD. invest;integrated security=false;user id=****;p assword=***** "Microsoft.entityframeworkcore.sqlserver-outputdir/ Filepath/models

Integrated Security (whether or not integrated authentication is the meaning of Windows account authentication)

Fourth step: Above we have seen that the context file has been generated, just need to generate an instance of it can be used.

Report:

Official documentation for configuring DbContext: Https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext

Fifth Step:

-outputdir Models: The directory where the files are generated, the current directory is the Models directory under the root directory

The engine then tries to connect to your SQL Server database and generate the files in the directory you specify.

Find a ***context.cs in the directory and open it, you will find a method like the following,

protected override void Onconfiguring (Dbcontextoptionsbuilder optionsbuilder) {  #warning to protect potentially Sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.  Optionsbuilder.usesqlserver (@ "{Your SQL connect string}");}

We should not put the connection string here, as is the case with the warning written in the auto-generated code. Next work, let's read the configuration from the Appsettings.json.

Add a property in/models/***context.cs to hold the ConnectionString, in addition we need to rewrite the Onconfiguring method "annotate the original method", the complete code should be this:

public static string ConnectionString {get; set;} protected override void Onconfiguring (Dbcontextoptionsbuilder optionsb Uilder) {   optionsbuilder.usesqlserver (ConnectionString);}

Open Appsetting.json and add the following code:

"ConnectionStrings": {   "Testnetcoreef": "Data source={your SQL Server host address};initial Catalog=testnetcoreef; User Id={your username};p assword={your password}; "},

Open Startup.cs and add the following code to the Configureservices (Iservicecollection Services) method:

testnetcoreefcontext.connectionstring = configuration.getconnectionstring ("Testnetcoreef");

The complete code should look like this:

public void Configureservices (iservicecollection services) {   //config the DB connection string   testnetcoreefcontext.connectionstring = configuration.getconnectionstring ("Testnetcoreef");    ADD Framework Services.   Services. Addmvc (); }

After the call, like EF,

. NET core uses EF core

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.