一、创建 ASP.NET Core WebApi项目
二、添加
三、
-----------------------------------------------------------
一、创建项目,WideWorldImporters.API,选项按照下列图操作
二、引用需要的Nuget包
- Microsoft.EntityFrameworkCore.SqlServer
Swashbuckle.AspNetCore
Swashbuckle.AspNetCore包允许为Web API启用帮助页。
试运行一下项目
OK, 没任何错误。????
添加一个文件夹Models,在里面添加4个.cs文件,
Entities.cs //实体,为了简单些把多个实体放在这一个文件里,实际项目里可以一个文件一个类
Extensions.cs //扩展类,
Requests.cs //请求类,
Responses.cs //响应类,
4个文件的完整代码如下:
Entities.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using Microsoft.EntityFrameworkCore; 6 using Microsoft.EntityFrameworkCore.Metadata.Builders; 7 8 namespace WideWorldImporters.API.Models 9 { 10 public partial class StockItem 11 { 12 public StockItem() 13 { 14 } 15 16 public StockItem(int? stockItemID) 17 { 18 StockItemID = stockItemID; 19 } 20 21 public int? StockItemID { get; set; } 22 23 public string StockItemName { get; set; } 24 25 public int? SupplierID { get; set; } 26 27 public int? ColorID { get; set; } 28 29 public int? UnitPackageID { get; set; } 30 31 public int? OuterPackageID { get; set; } 32 33 public string Brand { get; set; } 34 35 public string Size { get; set; } 36 37 public int? LeadTimeDays { get; set; } 38 39 public int? QuantityPerOuter { get; set; } 40 41 public bool? IsChillerStock { get; set; } 42 43 public string Barcode { get; set; } 44 45 public decimal? TaxRate { get; set; } 46 47 public decimal? UnitPrice { get; set; } 48 49 public decimal? RecommendedRetailPrice { get; set; } 50 51 public decimal? TypicalWeightPerUnit { get; set; } 52 53 public string MarketingComments { get; set; } 54 55 public string InternalComments { get; set; } 56 57 public string CustomFields { get; set; } 58 59 public string Tags { get; set; } 60 61 public string SearchDetails { get; set; } 62 63 public int? LastEditedBy { get; set; } 64 65 public DateTime? ValidFrom { get; set; } 66 67 public DateTime? ValidTo { get; set; } 68 } 69 70 public class StockItemsConfiguration : IEntityTypeConfiguration<StockItem> 71 { 72 public void Configure(EntityTypeBuilder<StockItem> builder) 73 { 74 // Set configuration for entity 75 builder.ToTable("StockItems", "Warehouse"); 76 77 // Set key for entity 78 builder.HasKey(p => p.StockItemID); 79 80 // Set configuration for columns 81 82 builder.Property(p => p.StockItemName).HasColumnType("nvarchar(200)").IsRequired(); 83 builder.Property(p => p.SupplierID).HasColumnType("int").IsRequired(); 84 builder.Property(p => p.ColorID).HasColumnType("int"); 85 builder.Property(p => p.UnitPackageID).HasColumnType("int").IsRequired(); 86 builder.Property(p => p.OuterPackageID).HasColumnType("int").IsRequired(); 87 builder.Property(p => p.Brand).HasColumnType("nvarchar(100)"); 88 builder.Property(p => p.Size).HasColumnType("nvarchar(40)"); 89 builder.Property(p => p.LeadTimeDays).HasColumnType("int").IsRequired(); 90 builder.Property(p => p.QuantityPerOuter).HasColumnType("int").IsRequired(); 91 builder.Property(p => p.IsChillerStock).HasColumnType("bit").IsRequired(); 92 builder.Property(p => p.Barcode).HasColumnType("nvarchar(100)"); 93 builder.Property(p => p.TaxRate).HasColumnType("decimal(18, 3)").IsRequired(); 94 builder.Property(p => p.UnitPrice).HasColumnType("decimal(18, 2)").IsRequired(); 95 builder.Property(p => p.RecommendedRetailPrice).HasColumnType("decimal(18, 2)"); 96 builder.Property(p => p.TypicalWeightPerUnit).HasColumnType("decimal(18, 3)").IsRequired(); 97 builder.Property(p => p.MarketingComments).HasColumnType("nvarchar(max)"); 98 builder.Property(p => p.InternalComments).HasColumnType("nvarchar(max)"); 99 builder.Property(p => p.CustomFields).HasColumnType("nvarchar(max)"); 100 builder.Property(p => p.LastEditedBy).HasColumnType("int").IsRequired(); 101 102 // Computed columns 103 104 builder 105 .Property(p => p.StockItemID) 106 .HasColumnType("int") 107 .IsRequired() 108 .HasComputedColumnSql("NEXT VALUE FOR [Sequences].[StockItemID]"); 109 110 builder 111 .Property(p => p.Tags) 112 .HasColumnType("nvarchar(max)") 113 .HasComputedColumnSql("json_query([CustomFields],N'$.Tags')"); 114 115 builder 116 .Property(p => p.SearchDetails) 117 .HasColumnType("nvarchar(max)") 118 .IsRequired() 119 .HasComputedColumnSql("concat([StockItemName],N' ',[MarketingComments])"); 120 121 // Columns with generated value on add or update 122 123 builder 124 .Property(p => p.ValidFrom) 125 .HasColumnType("datetime2") 126 .IsRequired() 127 .ValueGeneratedOnAddOrUpdate(); 128 129 builder 130 .Property(p => p.ValidTo) 131 .HasColumnType("datetime2") 132 .IsRequired() 133 .ValueGeneratedOnAddOrUpdate(); 134 } 135 } 136 137 public class WideWorldImportersDbContext : DbContext 138 { 139 public WideWorldImportersDbContext(DbContextOptions<WideWorldImportersDbContext> options) 140 : base(options) 141 { 142 } 143 144 protected override void OnModelCreating(ModelBuilder modelBuilder) 145 { 146 // Apply configurations for entity 147 148 modelBuilder 149 .ApplyConfiguration(new StockItemsConfiguration()); 150 151 base.OnModelCreating(modelBuilder); 152 } 153 154 public DbSet<StockItem> StockItems { get; set; } 155 } 156 }