1 创建项目
创建一个MyWebApp的空项目。也可以用一下命令行创建。
dotnet new globaljson --sdk-version 3.1 --output MyWebApp
已成功创建模板“global.json file”。
dotnet new web --no-https --output MyWebApp --framework netcoreapp3.1
已成功创建模板“ASP.NET Core Empty”。
dotnet new sln -o MyWebApp
已成功创建模板“解决方案文件”。
dotnet sln MyWebApp add MyWebApp
已将项目“WebApp.csproj”添加到解决方案中。
2 添加数据模型
2.1 添加数据库NuGet包
Microsoft.EntityFrameworkCore.Design
3.1.1版本
Microsoft.EntityFrameworkCore.SqlServer
3.1.1版本
2.2 创建数据模型
在Models文件夹中创建三个类:Product、Supplier、Category。
/// <summary>
/// 产品
/// </summary>
public class Product
{
public long ProductId { get; set; }
public string Name { get; set; }
[Column(TypeName = "decimal(8, 2)")]
public decimal Price { get; set; }
public long CategoryId { get; set; }
public Category Category { get; set; }
public long SupplierId { get; set; }
public Supplier Supplier { get; set; }
}
/// <summary>
/// 供应商
/// </summary>
public class Supplier
{
public long SupplierId { get; set; }
public string Name { get; set; }
public string City { get; set; }
public IEnumerable<Product> Products { get; set; }
}
/// <summary>
/// 类别
/// </summary>
public class Category
{
public long Categor