tnblog
首页
视频
资源
登录

.net Core From DataBase Layer To UI Layer

4376人阅读 2019/7/12 16:16 总访问:2537823 评论:2 收藏:0 手机
分类: .net后台框架

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.


欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739

评价
这一世以无限游戏为使命!
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术