1. Connection string Read
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.
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.
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