From a84a6341b7290b11d4b1c88eb9b095c010654679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E5=98=89?= <311226899@qq.com> Date: Thu, 11 Jun 2020 17:32:56 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=B8=E5=98=89=E3=80=82=E3=80=82=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../imolementation/EfRespository.cs" | 0 .../WebApi0003/interface/IRespository.cs" | 0 .../\350\256\270\345\230\211/WebApi0003.sln" | 25 ++++++ .../WebApi0003/Animals.cs" | 32 ++++++++ .../WebApi0003/Controllers/TestController.cs" | 29 +++++++ .../Controllers/WeatherForecastController.cs" | 60 ++++++++++++++ .../WebApi0003/Domain/Admin3200DbContext.cs" | 27 +++++++ .../WebApi0003/Domain/BaseEntity.cs" | 31 ++++++++ .../WebApi0003/Domain/DbInitializer.cs" | 48 +++++++++++ .../WebApi0003/Domain/Entity/Roles.cs" | 15 ++++ .../WebApi0003/Domain/Entity/Users.cs" | 21 +++++ .../WebApi0003/Helper/JsonHelper.cs" | 20 +++++ .../WebApi0003/Program.cs" | 26 ++++++ .../Properties/launchSettings.json" | 30 +++++++ .../WebApi0003/Startup.cs" | 55 +++++++++++++ .../WebApi0003/WeatherForecast.cs" | 15 ++++ .../WebApi0003/WebApi0003.csproj" | 22 ++++++ .../WebApi0003/appsettings.Development.json" | 9 +++ .../WebApi0003/appsettings.json" | 10 +++ .../imolementation/EfRespository.cs" | 79 +++++++++++++++++++ .../WebApi0003/interface/IRespository.cs" | 23 ++++++ 21 files changed, 577 insertions(+) rename "\350\256\270\345\230\211/WebApi0003/Helper/imolementation/EfRespository.cs" => "\350\256\270\345\230\211/WebApi0003/imolementation/EfRespository.cs" (100%) rename "\350\256\270\345\230\211/WebApi0003/Helper/interface/IRespository.cs" => "\350\256\270\345\230\211/WebApi0003/interface/IRespository.cs" (100%) create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003.sln" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Animals.cs" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Controllers/TestController.cs" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Controllers/WeatherForecastController.cs" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/Admin3200DbContext.cs" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/BaseEntity.cs" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/DbInitializer.cs" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/Entity/Roles.cs" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/Entity/Users.cs" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Helper/JsonHelper.cs" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Program.cs" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Properties/launchSettings.json" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Startup.cs" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/WeatherForecast.cs" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/WebApi0003.csproj" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/appsettings.Development.json" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/appsettings.json" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/imolementation/EfRespository.cs" create mode 100644 "\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/interface/IRespository.cs" diff --git "a/\350\256\270\345\230\211/WebApi0003/Helper/imolementation/EfRespository.cs" "b/\350\256\270\345\230\211/WebApi0003/imolementation/EfRespository.cs" similarity index 100% rename from "\350\256\270\345\230\211/WebApi0003/Helper/imolementation/EfRespository.cs" rename to "\350\256\270\345\230\211/WebApi0003/imolementation/EfRespository.cs" diff --git "a/\350\256\270\345\230\211/WebApi0003/Helper/interface/IRespository.cs" "b/\350\256\270\345\230\211/WebApi0003/interface/IRespository.cs" similarity index 100% rename from "\350\256\270\345\230\211/WebApi0003/Helper/interface/IRespository.cs" rename to "\350\256\270\345\230\211/WebApi0003/interface/IRespository.cs" diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003.sln" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003.sln" new file mode 100644 index 0000000..c3e1c29 --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003.sln" @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30011.22 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApi0003", "WebApi0003\WebApi0003.csproj", "{17DFBBDC-5C5D-4823-BE08-1A1C19ABD5F8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {17DFBBDC-5C5D-4823-BE08-1A1C19ABD5F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {17DFBBDC-5C5D-4823-BE08-1A1C19ABD5F8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {17DFBBDC-5C5D-4823-BE08-1A1C19ABD5F8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {17DFBBDC-5C5D-4823-BE08-1A1C19ABD5F8}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {97A5B6B1-659A-4BF8-813C-35777A3D8512} + EndGlobalSection +EndGlobal diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Animals.cs" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Animals.cs" new file mode 100644 index 0000000..4fad45d --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Animals.cs" @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApi0003 +{ + public class Animals + { + public virtual void Eat() + { + Console.WriteLine("动物世界"); + } + } + public class Dog : Animals + { + public override void Eat() + { + Console.WriteLine("小狗狗,听话"); + } + } + + public class Cat : Animals + { + public override void Eat() + { + Console.WriteLine("小猫咪,咪咪咪"); + } + } + + +} diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Controllers/TestController.cs" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Controllers/TestController.cs" new file mode 100644 index 0000000..5a9dd60 --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Controllers/TestController.cs" @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using WebApi0003.Domain.Entity; +using WebApi0003.Helper; +using WebApi0003.Helper.imolementation; + +namespace WebApi0003.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class TestController : ControllerBase + { + private readonly IRespository _usersRepository; + + public TestController(IRespository usersRepository) + { + _usersRepository = usersRepository; + } + public string Get() + { + var list = _usersRepository.Table.ToList(); + return JsonHelper.SerializeObject(list); + } + } +} \ No newline at end of file diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Controllers/WeatherForecastController.cs" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Controllers/WeatherForecastController.cs" new file mode 100644 index 0000000..7e968a7 --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Controllers/WeatherForecastController.cs" @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using WebApi0003.Domain; +using WebApi0003.Domain.Entity; +using Microsoft.Extensions.DependencyInjection; +using System.Data.Common; +using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json; +using WebApi0003.Helper; + +namespace WebApi0003.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + + private readonly Admin3200DbContext _db; + + //[ActivatorUtilitiesConstructor] + //public WeatherForecastController(Admin3200DbContext dbContext) + //{ + // _db = dbContext; + + //} + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger,Admin3200DbContext dbContext) + { + _logger = logger; + _db = dbContext; + } + + [HttpGet] + public string Get() + { + //var rng = new Random(); + //return Enumerable.Range(1, 5).Select(index => new WeatherForecast + //{ + // Date = DateTime.Now.AddDays(index), + // TemperatureC = rng.Next(-20, 55), + // Summary = Summaries[rng.Next(Summaries.Length)] + //}) + //.ToArray(); + var res = _db.Users.Include(x=>x.Roles).ToList(); + + return JsonHelper.SerializeObject(res); + } + } + } + diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/Admin3200DbContext.cs" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/Admin3200DbContext.cs" new file mode 100644 index 0000000..fd8f1e5 --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/Admin3200DbContext.cs" @@ -0,0 +1,27 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApi0003.Domain.Entity; + +namespace WebApi0003.Domain +{ + public class Admin3200DbContext:DbContext + { + public Admin3200DbContext() + { + + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + var connectionString = "server=.;database=Admin3200;uid=sa;pwd=123456;"; + optionsBuilder.UseSqlServer(connectionString); + } + + public DbSet Users { get; set; } + + public DbSet Roles { get; set; } + } +} diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/BaseEntity.cs" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/BaseEntity.cs" new file mode 100644 index 0000000..7fe5ddd --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/BaseEntity.cs" @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApi0003.Domain +{ + public class BaseEntity + { + public BaseEntity() + { + IsActived = true; + IsDeled = false; + CreateTime = DateTime.Now; + UpdatedTime = DateTime.Now; + } + public int Id { get; set; } + + public bool IsActived { get; set; } + + public bool IsDeled { get; set; } + + public DateTime CreateTime { get; set; } + + public DateTime UpdatedTime { get; set; } + + public int DisplayOrder { get; set; } + + public string Remarks { get; set; } + } +} diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/DbInitializer.cs" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/DbInitializer.cs" new file mode 100644 index 0000000..bfa036b --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/DbInitializer.cs" @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApi0003.Domain.Entity; + +namespace WebApi0003.Domain +{ + public class DbInitializer + { + public static void Seed() + { + using var db = new Admin3200DbContext(); + db.Database.EnsureCreated(); + + var hasUser = db.Users.Any(); + + if (!hasUser) + { + var role = new Roles + { + RoleName = "管理员", + Description = "管理员是我???" + }; + db.Roles.Add(role); + + db.SaveChanges(); + + db.Users.AddRange(new Users[] + { + new Users + { + Username="abd", + Password="123456", + RolesId=role.Id + }, + new Users + { + Username="abd01", + Password="abcdef", + RolesId=role.Id + } + }); + db.SaveChanges(); + } + } + } +} diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/Entity/Roles.cs" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/Entity/Roles.cs" new file mode 100644 index 0000000..078daf4 --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/Entity/Roles.cs" @@ -0,0 +1,15 @@ +using System.Collections.Generic; + +namespace WebApi0003.Domain.Entity +{ + public class Roles : BaseEntity + { + public string RoleName { get; set; } + + public string Description { get; set; } + + public IEnumerable Users { get; set; } + + } + +} \ No newline at end of file diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/Entity/Users.cs" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/Entity/Users.cs" new file mode 100644 index 0000000..a756cc1 --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Domain/Entity/Users.cs" @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApi0003.Domain.Entity +{ + public class Users:BaseEntity + { + public string Username { get; set; } + + + public string Password { get; set; } + + public int RolesId { get; set; } + + public Roles Roles { get; set; } + + + } +} diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Helper/JsonHelper.cs" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Helper/JsonHelper.cs" new file mode 100644 index 0000000..38e63dd --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Helper/JsonHelper.cs" @@ -0,0 +1,20 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApi0003.Helper +{ + public class JsonHelper + { + public static string SerializeObject(object obj) + { + return JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings + { + ReferenceLoopHandling = ReferenceLoopHandling.Ignore, + DateFormatString = "yyy-MM-dd HH:mm:ss" + }); + } + } +} diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Program.cs" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Program.cs" new file mode 100644 index 0000000..2add2e7 --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Program.cs" @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace WebApi0003 +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Properties/launchSettings.json" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Properties/launchSettings.json" new file mode 100644 index 0000000..e08633a --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:36839", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "WebApi0003": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Startup.cs" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Startup.cs" new file mode 100644 index 0000000..d8199af --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/Startup.cs" @@ -0,0 +1,55 @@ +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 WebApi0003.Domain; +using WebApi0003.Helper.imolementation; + +namespace WebApi0003 +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddDbContext(); + + services.AddScoped(typeof(IRespository<>), typeof(EfRespository<>)); + + services.AddControllers(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + DbInitializer.Seed(); + } + } +} diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/WeatherForecast.cs" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/WeatherForecast.cs" new file mode 100644 index 0000000..ee032e7 --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/WeatherForecast.cs" @@ -0,0 +1,15 @@ +using System; + +namespace WebApi0003 +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string Summary { get; set; } + } +} diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/WebApi0003.csproj" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/WebApi0003.csproj" new file mode 100644 index 0000000..bd9188c --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/WebApi0003.csproj" @@ -0,0 +1,22 @@ + + + + netcoreapp3.1 + + + + + + + + + + + + + + + + + + diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/appsettings.Development.json" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/appsettings.Development.json" new file mode 100644 index 0000000..8983e0f --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/appsettings.json" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/appsettings.json" new file mode 100644 index 0000000..d9d9a9b --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/appsettings.json" @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/imolementation/EfRespository.cs" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/imolementation/EfRespository.cs" new file mode 100644 index 0000000..522cc2e --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/imolementation/EfRespository.cs" @@ -0,0 +1,79 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApi0003.Domain; + +namespace WebApi0003.Helper.imolementation +{ + public class EfRespository : IRespository where T : BaseEntity + { + private readonly Admin3200DbContext db; + + private DbSet _entity; + + protected DbSet Entity { get + { + if (_entity == null) + { + _entity = db.Set(); + } + return _entity; + } + } + public IQueryable Table + { + get + { + return Entity; + } + } + + + public EfRespository(Admin3200DbContext dbContext) + { + db=dbContext; + } + public void Delete(int id) + { + var row= Table.Where(x => x.Id == id).FirstOrDefault(); + Delete(row); + } + + public void Delete(T entity) + { + this._entity.Remove(entity); + + db.SaveChanges(); + } + + + + public T GetById(int id) + { + return _entity.Where(x => x.Id == id).FirstOrDefault(); + } + + public void InserBulk(IEnumerable list) + { + _entity.AddRange(list); + db.SaveChanges(); + } + + public void Insert(T entity) + { + _entity.Add(entity); + db.SaveChanges(); + } + + + public void Update(T entity) + { + _entity.Add(entity); + db.SaveChanges(); + } + + + } +} diff --git "a/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/interface/IRespository.cs" "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/interface/IRespository.cs" new file mode 100644 index 0000000..3bbc918 --- /dev/null +++ "b/\350\260\242\346\261\266\351\234\226/\350\256\270\345\230\211/WebApi0003/interface/IRespository.cs" @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApi0003.Helper.imolementation +{ + public interface IRespository + { + IQueryable Table { get; } + T GetById(int id); + + void Insert(T entity); + + void InserBulk(IEnumerable list); + + void Update(T entity); + + void Delete(T entity); + + void Delete(int id); + } +} -- Gitee