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.