Use asp.net core 2.0 + EFCore. Sqlite to create a small website, coreefcore. sqlite

Source: Internet
Author: User

Use asp.net core 2.0 + EFCore. Sqlite to create a small website, coreefcore. sqlite

C # is not used to write programs for a long time. I heard that something was going on, and NetCore2.0 was released. So I learned about it and the site came into being.

In most cases, you can start from the official documents. Here are some of the pitfalls you may encounter.

First, this site is based on ASP. NetCore2.0 and EntityFrameWorkCore. Sqlite. The front end uses layui and is built on CentOS7.

ASP. NetCore has no difficulty. The MVC method is basically the same as the original asp.net mvc method. But I haven't used mvc for a long time, so I found out that tagHelper is very useful.

This article focuses on EFC, which is currently EFC2.0 and does not support latency queries. Therefore, each table associated query must Include (). Then, you cannot use many-to-many operations for table joining. However, you can add intermediate classes for many-to-many operations.

For example, an article has multiple categories, and one category also has multiple articles. Many-to-many associations are used here, so I added an intermediate association type, including the document and category ID. Two one-to-many associations are used to avoid multiple-to-many associations.

EF also has a console command dotnet ef

Make the. NetCore2.0 website project support EF. Sqlite

1) Add etettings. json and configure the sqlite connection string

"Sqlitedb": "Filename =./sqlitedb. db"

Note: After adding appsettings. json, set the property to copy to the output directory.

2) enable the Service in Startup

Add reference:

Using Microsoft. Extensions. Configuration;

Using Microsoft. EntityFrameworkCore;

Add attributes:

Public IConfiguration Configuration {get; set ;}

Add structure:

Public Startup ()

{

Var builder = new ConfigurationBuilder ()

. AddJsonFile ("etettings. json ");

Configuration = builder. Build ();

}

Enable ConfigureServices:

Var connection = Configuration. GetConnectionString ("sqlitedb ");

Services. AddDbContextPool <DataContext> (options => options. UseSqlite (connection ));

The DataContext class is missing. Create it in the Data folder.

Support Identity

1) EDIT DataContext

Add reference:

Using Microsoft. AspNetCore. Identity. EntityFrameworkCore;

Using Microsoft. EntityFrameworkCore;

Inherit IdentityDbContext <User>

Add non-default constructor:

Public DataContext (DbContextOptions <DataContext> options)

: Base (options)

{

}

2) The User class is missing. Create it in the Model folder and inherit from IdentityUser.

Add reference to the User class:

Using Microsoft. AspNetCore. Identity;

3) in the Startup class

Start the service in ConfigureServices:

Services. AddMvc ();

Services. AddIdentity <User, IdentityRole> (). AddEntityFrameworkStores <DataContext> (). adddefatotokenproviders ();

Perform authentication in the Configure () function of Pipeline:

App. UseAuthentication ();

Then, you can use the [Authorize] label in the Controller to verify the logon identity.

Use EF tool to generate database structure

1) Right-click to edit the project file csproj

Add in ItemGroup

<DotNetCliToolReference Include = "Microsoft. EntityFrameworkCore. Tools. DotNet" Version = "2.0.0"/>

2) Open the directory where the project is located, hold down shift, right-click the outbound menu, and open the command line

Enter:

Dotnet ef migrations add InitialCreate

Replaceable name

If an error occurs, analyze the cause.

A Migrations file is generated under the project, which contains the database structure code.

Enter the following command and press enter to make it take effect:

Dotnet ef database update

Then the database file configured in etettings. json will appear.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.