tnblog
首页
登录

.net Core From DataBase Layer To UI Layer

164人阅读 2019/7/12 16:16 评论:2 手机 收藏 关注
分类: .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.

评价
有钱人的开发有着你们不懂的快乐!O(* ̄︶ ̄*)O
文章
6
粉丝
16
评论
8
分类
16
{{item.ArticleTitle}}
{{item.BlogName}} : {{item.Content}}