一:首先,我们创建好项目的文件夹,然后进入文件夹,创建一个web的应用程序,具体用到的命令是“dotnet new”,dotnet new – 根据指定的模板,创建新的项目、配置文件或解决方案。常用的有类库“classlib”、控制台应用程序“console”、WebAPI应用“webapi”、ASP.NET Core Web 应用程序“webapp”
using(var context =new MySqlTestDBContext()){var blog =new Blog {Url="http://example.com"};var Id =context.Blogs.Add(blog).Entity.Id;context.SaveChanges();}
using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations.Schema;namespace MySqlTest.Models{publicclassBlog{[Column("BlogId")]publicint Id {get;set;}[Column("Url")]publicstring Url {get;set;}public List<Post> Posts {get;}=new List<Post>();}}
using System;using System.ComponentModel.DataAnnotations.Schema;namespace MySqlTest.Models{publicclassPost{[Column("PostId")]publicint Id {get;set;}[Column("Title")]publicstring Title {get;set;}[Column("Content")]publicstring Content {get;set;}}}
using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations.Schema;namespace MySqlTest.Models{publicclassBlog{[Column("BlogId")]publicint Id {get;set;}[Column("Url")]publicstring Url {get;set;}[Column("Title")]publicstring Title {get;set;}public List<Post> Posts {get;}=new List<Post>();}}
using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations.Schema;namespace MySqlTest.Models{publicclassPerson{[Column("PersonId")]publicint Id {get;set;}[Column("CelPhoneNo")]publicstring CelPhoneNo {get;set;}[Column("Name")]publicstring Name {get;set;}}}
然后,在上下文里面添加这个属性 public DbSet<Person> Persons { get; set; }
using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Hosting;using Microsoft.Extensions.Logging;using Microsoft.OpenApi.Models;using Swashbuckle.AspNetCore.Swagger;using System.IO;namespace MYun.BPC.WebAPI.Basic.Action{publicclassStartup{publicStartup(IConfiguration configuration){Configuration=configuration;}public IConfiguration Configuration {get;}// This method gets called by the runtime. Use this method to add services to the container.publicvoidConfigureServices(IServiceCollection services){services.AddControllers();services.AddSwaggerGen(c =>{c.SwaggerDoc("v1",new OpenApiInfo{Version="v1",//接口文档版本Title="Myun .NetCore BascQuery API",//接口文档标题Description="基础模块BaseQuery",//描述,不同模块自己设计});// 为 Swagger JSON and UI设置xml文档注释路径var basePath =Path.GetDirectoryName(typeof(Program).Assembly.Location);//获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径)var ServicexmlPath =Path.Combine(basePath,"Service.xml");//此为框架契约说明文档c.IncludeXmlComments(ServicexmlPath);var DataxmlPath =Path.Combine(basePath,"MYun.MY.Contract.Data.xml");//此为返回对象说明文档c.IncludeXmlComments(DataxmlPath);var xmlPath =Path.Combine(basePath,"MYun.BPC.WebAPI.Basic.Query.xml");//此为当前站点说明文档,Query或Action按不同站点具体配置c.IncludeXmlComments(xmlPath);});}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.publicvoidConfigure(IApplicationBuilder app, IWebHostEnvironment env){if(env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseRouting();app.UseAuthorization();app.UseEndpoints(endpoints =>{endpoints.MapControllers();});//启用中间件服务生成Swagger作为JSON终结点app.UseSwagger();//启用中间件服务对swagger-ui,指定Swagger JSON终结点app.UseSwaggerUI(c =>{c.SwaggerEndpoint("/swagger/v1/swagger.json","Myun .NetCore BascQuery API");});}}}