Efcore Configuring linked SQL Server

Source: Internet
Author: User

This article will use the Efcore CoreFirst mode in the ASP. NET Core API project to simply configure the linked SQL Server database, as well as simple database migration operations



A new project

1. First we create an empty ASP. NET Core API project with vs2017


2. Under the generated solution, in establishing a class library to access the database Coreapi.model, be careful to select the class library under. Netcore,


Two add related references

1. Open the NuGet Package Program Management Command Console, execute the Add Reference command, note that the default project of the console is to be positioned as Coreapi.model when executing

Reference Entityframeworkcore

Install-package Microsoft.entityframeworkcore

Reference Entityframeworkcore.sqlserver

Install-package Microsoft.EntityFrameworkCore.SqlServer


Reference EntityFrameworkCore.SqlServer.Tools

Install-package Microsoft.EntityFrameworkCore.Tools




Three related configurations

1. Add the SQL Server database link configuration in the Appsettings.json file, configured as follows

{"  ConnectionStrings": {    "sqlserverconnection": "Server=.;D Atabase=dbcore; User Id=sa; password=abc123456; "  },  " Logging ": {    " includescopes ": false,    " LogLevel ": {      " Default ":" Warning "    }  }}


2. Modify the Configureservices method of the project Startup.cs file, note that the using Microsoft.entityframeworkcore must be referenced here

and using Coreapi.model;

This method gets called by the runtime. Use this method to add services to the container.        public void Configureservices (iservicecollection services)        {            var sqlConnection = Configuration.getconnectionstring ("Sqlserverconnection");            Services. adddbcontext<apidbcontent> (option = option. Usesqlserver (sqlConnection));            Services. Addmvc ();        }


Quad Build Database


    1. First built on the UserInfo user model under Coreapi.model

Public  class UserInfo    {public             int Id {get; set;}            public string UserName {get; set;}            public string Password {get; set;}    }


2. Configure the data context

public class Apidbcontent:dbcontext        {public            apidbcontent (dbcontextoptions<apidbcontent> options)                : Base (options)            {            } public            dbset<userinfo> Users {get; set;}        }

3. Open the Package management console, execute the add-migration Migrations Command, and note that the default project must also locate Coreapi.model at this time,

If it goes well, a migrations folder should be generated under the project and contain some files to initialize the build database


4. Execute the update-database command to generate the database,




Five Simple database migrations
    1. We added a new articles model to add two fields to the UserInfo, update it to the database using a command, or need to be aware that the default project must also locate Coreapi.model because all of our database operations are for the Model layer

Public  class UserInfo    {public             int Id {get; set;}            public string UserName {get; set;}            public string Password {get; set;}            public string Phone {get; set;}            Public virtual list<articles> articles {get; set;}    }    public class articles    {public        int Id {get; set;}        public string Title {get; set;}        public string Summary {get; set;}        Public virtual UserInfo  user{get;set;}    }
    1. We execute the command add-migration migrationname and update-datebase after the successful refresh of the database can see the table and the fields are correspondingly added, of course, there are foreign keys


Six finally we initialize some data in the database and then use Efcore to request the data

1. Initial user data in the database


2. Constructor mode initializes the data context and simply modifies the Get method under Valuescontroller

Private ReadOnly apidbcontent _dbcontext;         Public Valuescontroller (apidbcontent dbContext)        {            _dbcontext = dbContext;        }        Get api/values        [httpget] public        jsonresult Get ()        {            return Json (_dbcontext.users.take (2). ToList ());            return new string[] {"value1", "value2"};        }

3. Start the project request get API


Bo main website:http://www.siyouku.cn

This document is permanent and more detailed address: http://siyouku.cn/article/6818.html


Efcore Configuring linked SQL Server

Related Article

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.