Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use EntityFramework Core for persistent configuration in IdentityServer4

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use EntityFramework Core for persistent configuration in IdentityServer4. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Generally speaking, EF will be used for migration, so if you let EF support the .NET Cli command line, open the project.

Add a bit of code before to support the Cli command line, after which you can go to the project root directory to execute doenet ef through cmd.exe, if there is no error, ok! If there is a problem, it is mostly in the wrong place.

Given the flexibility of EF, we use the SqlServer database for this article, so let's create a database and some related tables.

Maintains the latest SQL script for SqlServer for entities in IdentityServer4.EntityFramework.Storage. They're right here. (just create a database and put those two tables into it for execution.)

Next we need to configure in Server, we need to modify the method ConfigureServices in Startup.cs, first we define a constant connectionString, which you must know, this is a database connection string, and then define a migrationsAssembly, which gets the name of the assembly, and then assigns the relevant migration configuration of the database through AddIdentityServer and AddOperationalStore methods.

In the meantime, you will refer to System.Reflection and Microsoft.EntityFrameworkCore, as mentioned above, the above is configured in hard-coded form, more specifically, the configuration about the database is an object called ConfigurationStoreOptions, you can modify it freely, EF-compatible databases are all available. Because IdentityServer4 implements interfaces that can be implemented by EF.

Where did it come from? It is not difficult to find that there is a parameter called AddOperationalStore in the definition of IdentityServerEntityFrameworkBuilderExtensions, which is to add a dynamic repository, as defined below.

It can be seen that it implements DbContext, and we are all .NET programmers, so I think you should know what's going on. Now, make sure your connection string is correct, and we're going to start the migration! Execute the following command.

If there is no problem, the result must be as follows, of course, you must install Microsoft.EntityFrameworkCore in your project, otherwise there will be ERROR:Microsoft.EntityFrameworkCore.Metadata.Internal.DirectConstructorBinding.

You should now see a ~ / Data/Migrations/IdentityServer folder in the project. This contains the code for the newly created migration. Now that we have added the migration, we can write code to create the database from the migration. We will also seed the database using the memory configuration data we defined in the previous Quick start. Add this method in Startup.cs to help initialize the database:

The above code may need to add these namespaces to your file:

Using IdentityServer4.EntityFramework.DbContexts

Using IdentityServer4.EntityFramework.Mappers

Then we can call it from the Configure method:

Public void Configure (IApplicationBuilder app) {

InitializeDatabase (app)

}

Now, if you are running the IdentityServer project, you should create a database and seed it using the Quick start configuration data. You should be able to use SQL Server Management Studio or Visual Studio to connect and check the data.

Finally, you can configure the GetUsers in your Config.cs according to this Client table. Of course, you can customize the fields of the database to adapt to your application. Of course, you can update the database without updating once. Of course, you can update the database through the relevant commands of EF. Then all update records are in the _ _ EFMigrationsHistory table.

So much for sharing about how to use EntityFramework Core for persistent configuration in IdentityServer4. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report