ASP.NET Core Services Framework Configuration EF - ASP.NET Core Basics Tutorial - simple tutorial, simple programming

Original: ASP.NET Configuration EF Core Services Framework - ASP.NET Core Basics Tutorial - simple tutorial, simple programming

ASP.NET Core Services Framework Configuration EF

In the previous chapter we learned about the basic working principle and the Entity Framework  DbContext , we have created a own  HelloWorldDBContext. This chapter we will say something about how to set up the framework of our EF to link to SQLite databases

EF service configuration framework

Make our DBContext EF framework can be up and running, we need to change the configuration of the application

We need to add a database connection string, so that we  HelloWorldDBContext know to which server and which database to query

We need to do the following:

  1. It will save the database connection string in a configuration file in JSON
  2. Similarly, also you need  Startup the class  ConfigureServices to add EF service method
  3. EF and ASP.NET MVC framework and frameworks, use dependency injection. In order to be able to inject, you need to know to use a variety of services EF framework runtime
  4. Of course, there is a simple configuration API can be used to add all the default services we need

Well, let's go

Double-click to open the  AppSettings.json file and add the following database connection string

"database": {
    "connection": "Data Source=blogging.db" } 

After the addition is complete, AppSettings.json the entire contents of the file are as follows

{
   "The Message" : "the Hello World \ the n-hello, simple tutorial, your website is www.twle.cn it!?" , "Database" : { "Connection" : "the Data Source = blogging.db" } }

We are using a relative path, at  SQLite the time used a relative path, the path associated with the main assembly applications.

In our  HelloWorld project, the main binary is  bin/Debug/netcoreapp2.1/ConsoleApp.SQLite.dll, therefore SQLite database will be located  bin/Debug/netcoreapp2.1/blogging.db in

Next, we double-click to open the  Startup.cs file, go to  Startupclass

Core we need to add some additional services to work for the Entity Framework

Specifically, we need to do two things:

  1. Add EF framework of services related to SQLite
  2. EF framework of DBContext tell us which class

All of which can by  IServiceCollection accomplished using the methods provided on the extension, as shown in the following procedure

public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddEntityFrameworkSqlite() .AddDbContext<HelloWorldDBContext> (options => options.UseSqlite(Configuration["database:connection"])); } 

Because EF framework and compatible with different types of databases, including non-relational databases, we need to tell EF Entity Framework add SQLite related services

Then also you need  AddDbContext() to tell us DBContext framework approach EF class, which may be suitably configured to facilitate instance of the class

AddDbContext() Method accepts a generic type parameter, we specify the  DBContext type of the derived class HelloWorldDbContext

In the  AddDbContext() process, we need to set our  DBContext options

Setting options can be  a lambda expression  is done, we need to do is tell us DBContext frame EF will go UseSqlite

So far, we configured EF framework service work is finally done, then we will learn how to set up the database, the database migration

Guess you like

Origin www.cnblogs.com/jiejiehencool/p/11101081.html