Entityframewok Core 1.1 Connection MSSQL Database detailed

Source: Internet
Author: User
Tags dotnet mssql postgresql

Recently in the study of ASP. NET core, which uses the Entity Framework core, for the Entity Framework core connected SQL Server database, use code frist to create a database, update the database to make a record.

Development tool: VS2017 RC

Net Core Version: 1.1

EF Version: 1.1

Create a project

Open VS2017 RC, new project, select ASP. NET Core Web application, fill in Project name, Solution name, choose Save location, Next

  

Then select the ASP. NET Core 1.1,web application, authentication selection is not authenticated, click OK

  

Then right-click the solution and choose Add A. NET core class library, which we named EntityFramework

  

Two add related dependencies in Fy.entityframework

Add dependencies in Fy.entityframework add Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.Tools

Right-click Fy.entityframework, select Manage NuGet packages, browse in open pages, search for more than two dependencies, and install

  

  

If you're using VS2015, you can add dependencies in a different way, That is, add the dependencies directly to the Project.json file, but I used the VS2017 build project, the project does not have this file, but instead of returning to the previous. csproj file, now I also did not find out how this file was added since the item, is directly added? Or what? Hope to have a friend to know to tell a sound!

If you are using PostgreSQL in this place, the dependencies you want to add are

    • Npgsql.EntityFrameworkCore.PostgreSQL

PostgreSQL data provides the base class library that supports EF core, which is the root of using the PostgreSQL database with EF core.

    • Npgsql.EntityFrameworkCore.PostgreSQL.Design

The primary key of the type using the GUID (which corresponds to the type UUID of the Postgre data) must be, and the primary key of the Int/long type is not added or problematic.

    • Microsoft.EntityFrameworkCore.Tools

EF core Tools, Codefirst database migration related operations must be.

Three write a configuration file

To add a simple user entity class to fy.entityframework, let's simply simulate the data

  

1 namespaceFY. EntityFramework2 {3      Public classUser4     {5          Public intId {Get;Set; }6          Public stringUserName {Get;Set; }7          Public stringPassword {Get;Set; }8     }9}

Then define a context action class named "Appdbcontext" and Inherit from DbContext

1 usingSystem.Collections.Generic;2 usingSystem.IO;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespaceFY. EntityFramework7 {8      Public classAppdbcontext:dbcontext9     {Ten          PublicAppdbcontext (dbcontextoptions<appdbcontext> options):Base(Options) One         { A  -         } -          PublicDbset<user> User {Get;Set; } the  -         protected Override voidonmodelcreating (ModelBuilder ModelBuilder) -         { -             Base. Onmodelcreating (ModelBuilder); +         } -     } +}

Adding a database connection string to Appsettings.json in Fy.web

1 {2   "ConnectionStrings": {3     "sqlserverconnection":"server=.;D Atabase=dbcore; User Id=sa; Password=sa123sa;"4   },5   "Logging": {6     "Includescopes":false,7     "LogLevel": {8       "Default":"Warning"9     }Ten   } One}

In the Configureservices method in Startup.cs, get the database connection string and add the database connection service

  

1          Public void configureservices (iservicecollection services) 2         {3             var sqlConnection = configuration.getconnectionstring ("  Sqlserverconnection"); 4             Services. adddbcontext<appdbcontext> (option = option. Usesqlserver (sqlConnection)); 5             Services. Addmvc (); 6         }

Note: This place must be added

1 using microsoft.entityframeworkcore;

Otherwise, option = option. Usesqlserver (sqlConnection) This place error, I was in this place for a long time.

Four Add a database migration file and create a database

Click Tools->nuget Package Manager, Packages Manager console

  Note: If your PowerShell version is 2.0, you will need to upgrade it to 3.0 or 4.0.

Select the default project as Fy.entityframework

In the Package Manager console, enter add-migration migrations

  

After the execution is complete, the Migrations folder is added under the Fy.entityframework project, which contains three files

  

Then execute update-database, after the execution is complete, view the database, discover the database and tables, and create the completion.

In addition to using the program console to add the database, there is a way to use the dotnet command, but this way I did not succeed, because when using EF Core1.1, when using the command of the dotnet EF command, you need to

Add Microsoft.EntityFrameworkCore.Tools.DotNet, but you will be prompted the package type ' Dotnetclitool ' is not supported, because. NET Core CLI commands (dotnet EF) are currently not available in. NET Core Tools MSBuild Alpha.

Five summary

Now that we have created the database using EF Core 1.1, the PostgreSQL database is created in the same way that the connection string and the added dependencies are different.

Spit Groove Myself, I found myself really not suitable for writing articles, Inertia is too big, but the memory is not good, so force yourself to write this kind of article, to avoid their own forget again, you can also record the pit of their own tread. OK, here it is, next article goodbye!!!

  

Entityframewok Core 1.1 Connection MSSQL database detailed

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.