Play to ASP. NET 5: Database connection string configuration and read

Source: Internet
Author: User

1. Connection string Read

    • 1.1 Preface

Originally I wanted to re-construct in the future, to say this question again. But a friend asked me: How to write the database connection string to Config.json and read?

It turns out that everyone is quite familiar with the XML format configuration file. However, the ASP. NET 5 project is replaced by the more popular JSON format when configuring the file. If you do not read the source code, only from the Identity template codes to see, still very confused.

    • 1.2config.json Code

In the Blogaspnet5.consoleapp Console Project (execute program), add the Config.json file with its code:

{    "Data": {        "Efcontext": {            "ConnectionString":"server=.;D Atabase=testdb; Uid=sa; pwd=123456;"        }    },    "EntityFramework": {        "Efcontext": {            "Connectionstringkey":"Data:EFContext:ConnectionString"        }    }}

1.2 Assembly Introduction

Project.json configuration in blogaspnet5.repository :

Instead of using this class library, you can write your own reading of the JSON key values.

    • Modify the context code
usingBlogASPNET5.Entity.Accounts;usingMicrosoft.Data.Entity;usingMicrosoft.Data.Entity.Metadata;usingMicrosoft.Framework.ConfigurationModel;namespaceblogaspnet5.repository.contexts{ Public classEfcontext:dbcontext { PublicDbset<role> Roles {Get;Set; }  PublicDbset<user> Users {Get;Set; }  PublicIConfiguration Configuration {Get;Set; } /// <summary>        ///reading the connection string from Config.json/// </summary>        /// <returns></returns>         Public stringgetconnstring () {Configuration=NewConfiguration (). Addjsonfile ("Config.json"); returnConfiguration.get ("Data:EFContext:ConnectionString"); }        protected Override voidonconfiguring (dbcontextoptions options) {options.        Usesqlserver (Getconnstring ()); }        protected Override voidonmodelcreating (ModelBuilder ModelBuilder) {//Many-to-one relationships and specifying foreign keysModelbuilder.entity<user> (). Manytoone (r = r.role, U = u.users). ForeignKey (f =F.roleid); }    }}


2. Summary

This article is a supplement to the previous article, but also to answer the question of the park friends. Of course, this is just a way of writing, there are more general wording ...

(today's time is not very idle, did not follow the "plan" to share!) Please be patient and follow up! )

Play to ASP. NET 5: Database connection string configuration and read

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.