尘叶心繁

.net Core From DataBase Layer To UI Layer

电脑版发表于:2019/7/12 16:16

Here we have created the database.

OK,let's GO!

First:

You need create a new .net core to web application!

And create a .net core library

 


Then you need to see if EFcore, EFDesign, EFTools have been installed in the project configuration. If not, install the following commands through Nuget

Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.2.6

Install-Package Microsoft.EntityFrameworkCore.Design -Version 2.2.6

Install-Package Microsoft.EntityFrameworkCore.Tools -Version 2.2.6

//EFCore self-contained program .You Can Ctrl+/


With these

You can execute the following commands to automatically generate the entity model under the Model folder.

Scaffold-DbContext "Server=.;database=bb;uid=sa;pwd=xxx" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

Ok! Now!

You can see that the physical model is built.

Then.

We need Create conntion string to appsettings.json ,like this!

"ConnectionStrings": {
    "BloggingDatabase": "Server=.;database=bb;uid=sa;pwd=xxx"
  },

Then we need DI   To startup.cs(ConfigureServices) 

var connection = Configuration.GetConnectionString("BloggingDatabase");
services.AddDbContextPool<INGoodsTrackerDBContext>(option =>
{
    if (!option.IsConfigured)
    {
        option.UseSqlServer(connection);
    }
});

I'm sure you're curious why I don't use the AddDbContext method here , but the AddDbContextPool method.

You can refer to this article recommended by my colleague Venzent.

https://neelbhatt.com/2018/02/27/use-dbcontextpooling-to-improve-the-performance-net-core-2-1-feature/

I won't say much.

Let's continue.


private LocalDBContext _Context { get; set; }
        public TestController(LocalDBContext Context)
        {
            _Context = Context;
        }

After these completions ,we can finish it.

Becareful:  The OnConfiguring method in the generated content. CS needs to be removed when using dependency injection.

关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢