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