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:
- It will save the database connection string in a configuration file in JSON
- Similarly, also you need
Startup
the classConfigureServices
to add EF service method - 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
- 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 isbin/Debug/netcoreapp2.1/ConsoleApp.SQLite.dll
, therefore SQLite database will be locatedbin/Debug/netcoreapp2.1/blogging.db
in
Next, we double-click to open the Startup.cs
file, go to Startup
class
Core we need to add some additional services to work for the Entity Framework
Specifically, we need to do two things:
- Add EF framework of services related to SQLite
- 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