X01.SportWeb: An Example for AspNetCore 2.0,

Source: Internet
Author: User

X01.SportWeb: An Example for AspNetCore 2.0,

When the new one comes, the old one will be forgotten. Learning AspNet Core 2.0 is painful without a good example. SportsStore in Pro ASP. NET Core MVC 2 is worth reading. You may wish to download and study it:

Preparation

1. UseUbuntuSystem

2. InstallationNetCore2.0

3. InstallVSCode

4. InstallationSqlite

Start work

1. Create a folder named MvcLab and open it with VSCode,ALT +', Open the terminal, enter the commandDotnet new slnGenerate a solution. Then enter the following command to generate x01.SportWeb and Its Test Project:

    mkdir x01.SportWeb   cd x01.SportWeb/  dotnet new mvc --auth Individual  cd ..  mkdir x01.SportTest  cd x01.SprotTest/  dotnet new xunit  cd ..  dotnet sln add x01.SportWeb/x01.SportWeb.csproj  dotnet sln add x01.SportTest/x01.SportTest.csproj    

2. Compare with the example of SportsStore.

Additional instructions

1. Use Ubuntu for cross-platform access. The -- auth Individual option is used to prepare for user management.

2. In x01.SportTest. csproj, add a reference to x01.SportWeb. csproj:

  <ItemGroup>    <ProjectReference Include="../x01.SportWeb/x01.SportWeb.csproj" />  </ItemGroup>

3. InApplicationDbContextAddPublic DbSet <Product> Products {get; set :}After properties, run the following command:

dotnet ef migration add AddProduct

Before updating the database, you must add the TempDbContextFactory class as follows:

using Microsoft.EntityFrameworkCore;using Microsoft.EntityFrameworkCore.Design;namespace x01.SportWeb.Data{    public class TempDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>    {        public ApplicationDbContext CreateDbContext(string[] args)        {            var builder = new DbContextOptionsBuilder<ApplicationDbContext>();            builder.UseSqlite("Data Source=app.db");            return new ApplicationDbContext(builder.Options);        }    }}

Manually annotate Migrations /*_AddProductAddForeignKey in. cs:

       // migrationBuilder.AddForeignKey(            //     name: "FK_AspNetUserTokens_AspNetUsers_UserId",            //     table: "AspNetUserTokens",            //     column: "UserId",            //     principalTable: "AspNetUsers",            //     principalColumn: "Id",            //     onDelete: ReferentialAction.Cascade);

Run the following command to update the database:

  dotnet ef database update

Other languages in the book "Pro ASP. NET Core MVC 2" are quite detailed.

 

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.