From 328077f5c89d4bee8c3ed57b8aab0fd4d7993175 Mon Sep 17 00:00:00 2001 From: hh <2607470919@qq.com> Date: Fri, 5 Jun 2020 16:28:38 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=A2=E6=B5=B7=E6=81=A9=E7=9A=84=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WebApplication1.sln" | 25 ++++++ .../Controllers/WeatherForecastController.cs" | 59 ++++++++++++ .../Domain/Admin3200DbContext.cs" | 27 ++++++ .../WebApplication1/Domain/BaseEntity.cs" | 31 +++++++ .../WebApplication1/Domain/DbInitializer.cs" | 51 +++++++++++ .../WebApplication1/Domain/Entity/Roles.cs" | 16 ++++ .../WebApplication1/Domain/Entity/Users.cs" | 18 ++++ .../WebApplication1/Helper/JsonHelper.cs" | 22 +++++ .../Implementation/EfRespository.cs" | 89 +++++++++++++++++++ .../Interface/IRespository.cs" | 27 ++++++ .../WebApplication1/Program.cs" | 26 ++++++ .../Properties/launchSettings.json" | 30 +++++++ .../WebApplication1/Startup.cs" | 53 +++++++++++ .../WebApplication1/WeatherForecast.cs" | 15 ++++ .../WebApplication1/WebApplication1.csproj" | 14 +++ .../appsettings.Development.json" | 9 ++ .../WebApplication1/appsettings.json" | 10 +++ 17 files changed, 522 insertions(+) create mode 100644 "\350\260\242\346\265\267\346\201\251/WebApplication1.sln" create mode 100644 "\350\260\242\346\265\267\346\201\251/WebApplication1/Controllers/WeatherForecastController.cs" create mode 100644 "\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/Admin3200DbContext.cs" create mode 100644 "\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/BaseEntity.cs" create mode 100644 "\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/DbInitializer.cs" create mode 100644 "\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/Entity/Roles.cs" create mode 100644 "\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/Entity/Users.cs" create mode 100644 "\350\260\242\346\265\267\346\201\251/WebApplication1/Helper/JsonHelper.cs" create mode 100644 "\350\260\242\346\265\267\346\201\251/WebApplication1/Implementation/EfRespository.cs" create mode 100644 "\350\260\242\346\265\267\346\201\251/WebApplication1/Interface/IRespository.cs" create mode 100644 "\350\260\242\346\265\267\346\201\251/WebApplication1/Program.cs" create mode 100644 "\350\260\242\346\265\267\346\201\251/WebApplication1/Properties/launchSettings.json" create mode 100644 "\350\260\242\346\265\267\346\201\251/WebApplication1/Startup.cs" create mode 100644 "\350\260\242\346\265\267\346\201\251/WebApplication1/WeatherForecast.cs" create mode 100644 "\350\260\242\346\265\267\346\201\251/WebApplication1/WebApplication1.csproj" create mode 100644 "\350\260\242\346\265\267\346\201\251/WebApplication1/appsettings.Development.json" create mode 100644 "\350\260\242\346\265\267\346\201\251/WebApplication1/appsettings.json" diff --git "a/\350\260\242\346\265\267\346\201\251/WebApplication1.sln" "b/\350\260\242\346\265\267\346\201\251/WebApplication1.sln" new file mode 100644 index 0000000..339004f --- /dev/null +++ "b/\350\260\242\346\265\267\346\201\251/WebApplication1.sln" @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29806.167 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApplication1", "WebApplication1\WebApplication1.csproj", "{403931E1-EC72-484A-9B94-7CA057D09DF7}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {403931E1-EC72-484A-9B94-7CA057D09DF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {403931E1-EC72-484A-9B94-7CA057D09DF7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {403931E1-EC72-484A-9B94-7CA057D09DF7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {403931E1-EC72-484A-9B94-7CA057D09DF7}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {920E464F-61D4-4563-90AA-B4B4277AFF7C} + EndGlobalSection +EndGlobal diff --git "a/\350\260\242\346\265\267\346\201\251/WebApplication1/Controllers/WeatherForecastController.cs" "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Controllers/WeatherForecastController.cs" new file mode 100644 index 0000000..1d2f934 --- /dev/null +++ "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Controllers/WeatherForecastController.cs" @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using WebApplication1.Domain; +using WebApplication1.Domain.Entity; +using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json; +using Microsoft.EntityFrameworkCore; +using WebApplication1.Helper; + +namespace WebApplication1.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\265\267\346\201\251/WebApplication1/Domain/Admin3200DbContext.cs" "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/Admin3200DbContext.cs" new file mode 100644 index 0000000..1db3051 --- /dev/null +++ "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/Admin3200DbContext.cs" @@ -0,0 +1,27 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApplication1.Domain.Entity; + +namespace WebApplication1.Domain +{ + public class Admin3200DbContext:DbContext + { + public Admin3200DbContext() + { + + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + var connectionStrig = "server=.;database=Admin3200;uid=sa;pwd=123456;"; + optionsBuilder.UseSqlServer(connectionStrig); + } + + public DbSet Users { get; set; } + + public DbSet Roles { get; set; } + } +} diff --git "a/\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/BaseEntity.cs" "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/BaseEntity.cs" new file mode 100644 index 0000000..0f32218 --- /dev/null +++ "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/BaseEntity.cs" @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApplication1.Domain +{ + public class BaseEntity + { + public BaseEntity() + { + IsActived = true; + IsDeleted = false; + CreatedTime = DateTime.Now; + UpdatedTime = DateTime.Now; + } + public int Id { get; set; } + + public bool IsActived { get; set; } + + public bool IsDeleted { get; set; } + + public DateTime CreatedTime { get; set; } + + public DateTime UpdatedTime { get; set; } + + public int DisplayOrder { get; set; } + + public string Remarks { get; set; } + } +} diff --git "a/\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/DbInitializer.cs" "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/DbInitializer.cs" new file mode 100644 index 0000000..42cd266 --- /dev/null +++ "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/DbInitializer.cs" @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApplication1.Domain.Entity; + +namespace WebApplication1.Domain +{ + public class DbInitializer + { + public static void Seed() + { + using var db = new Admin3200DbContext(); + + db.Database.EnsureCreated(); + + var hasUser = db.Users.Any(); + + if (!hasUser) + { + var roles = new Roles + { + RolesName = "管理员", + Description = "没错,这就是一个猪猪管理员" + }; + + db.Roles.Add(roles); + + db.SaveChanges(); + + db.Users.AddRange(new Users[] + { + new Users + { + Username="", + Password="", + RolesId=roles.Id + }, + new Users + { + Username="猪猪姗01号", + Password="123456", + RolesId=roles.Id + } + }); + //保存更改 + db.SaveChanges(); + } + } + } +} diff --git "a/\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/Entity/Roles.cs" "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/Entity/Roles.cs" new file mode 100644 index 0000000..aea6979 --- /dev/null +++ "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/Entity/Roles.cs" @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApplication1.Domain.Entity +{ + public class Roles:BaseEntity + { + public string RolesName { get; set; } + + public string Description { get; set; } + + public IEnumerable users { get; set; } + } +} diff --git "a/\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/Entity/Users.cs" "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/Entity/Users.cs" new file mode 100644 index 0000000..b42b827 --- /dev/null +++ "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Domain/Entity/Users.cs" @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApplication1.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\265\267\346\201\251/WebApplication1/Helper/JsonHelper.cs" "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Helper/JsonHelper.cs" new file mode 100644 index 0000000..ba6d335 --- /dev/null +++ "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Helper/JsonHelper.cs" @@ -0,0 +1,22 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApplication1.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\265\267\346\201\251/WebApplication1/Implementation/EfRespository.cs" "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Implementation/EfRespository.cs" new file mode 100644 index 0000000..948f954 --- /dev/null +++ "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Implementation/EfRespository.cs" @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApplication1.Domain; +using Microsoft.EntityFrameworkCore; +using WebApplication1.Interface; + +namespace WebApplication1.Implementation +{ + //实现接口 + public class EfRespository : IRespository where T : BaseEntity + { + private readonly Admin3200DbContext db; + + private DbSet _entity; + + private 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(T entity) + { + this._entity.Remove(entity); + + db.SaveChanges();//保存更改 + } + + public void Delete(int id) + { + var row = Table.Where(x => x.Id == id).FirstOrDefault(); + Delete(row); + } + + /*public IEnumerable GetAllEntity() + { + throw new NotImplementedException(); + }*/ + + public T GetById(int id) + { + return _entity.Where(x => x.Id == id).FirstOrDefault(); + } + + public void Insert(T entity) + { + _entity.Add(entity); + db.SaveChanges(); + } + + public void InsertBulk(IEnumerable list) + { + _entity.AddRange(list); + db.SaveChanges(); + } + + public void Update(T entty) + { + _entity.Update(entty); + db.SaveChanges(); + } + + int IRespository.Insert(T entity) + { + throw new NotImplementedException(); + } + } +} diff --git "a/\350\260\242\346\265\267\346\201\251/WebApplication1/Interface/IRespository.cs" "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Interface/IRespository.cs" new file mode 100644 index 0000000..399946f --- /dev/null +++ "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Interface/IRespository.cs" @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApplication1.Interface +{ + public interface IRespository + { + IQueryable Table { get; } + T GetById(int id); + + //IEnumerable GetAllEntity(); + + //添加 + int Insert(T entity); + + //批量插入 + void InsertBulk(IEnumerable list); + + void Update(T entity); + + void Delete(T entity); + + void Delete(int id); + } +} diff --git "a/\350\260\242\346\265\267\346\201\251/WebApplication1/Program.cs" "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Program.cs" new file mode 100644 index 0000000..c015cb7 --- /dev/null +++ "b/\350\260\242\346\265\267\346\201\251/WebApplication1/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 WebApplication1 +{ + 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\265\267\346\201\251/WebApplication1/Properties/launchSettings.json" "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Properties/launchSettings.json" new file mode 100644 index 0000000..13bba13 --- /dev/null +++ "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:63382", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "WebApplication1": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git "a/\350\260\242\346\265\267\346\201\251/WebApplication1/Startup.cs" "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Startup.cs" new file mode 100644 index 0000000..12f0c41 --- /dev/null +++ "b/\350\260\242\346\265\267\346\201\251/WebApplication1/Startup.cs" @@ -0,0 +1,53 @@ +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 WebApplication1.Domain; + +namespace WebApplication1 +{ + 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.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\265\267\346\201\251/WebApplication1/WeatherForecast.cs" "b/\350\260\242\346\265\267\346\201\251/WebApplication1/WeatherForecast.cs" new file mode 100644 index 0000000..11a0296 --- /dev/null +++ "b/\350\260\242\346\265\267\346\201\251/WebApplication1/WeatherForecast.cs" @@ -0,0 +1,15 @@ +using System; + +namespace WebApplication1 +{ + 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\265\267\346\201\251/WebApplication1/WebApplication1.csproj" "b/\350\260\242\346\265\267\346\201\251/WebApplication1/WebApplication1.csproj" new file mode 100644 index 0000000..da10d50 --- /dev/null +++ "b/\350\260\242\346\265\267\346\201\251/WebApplication1/WebApplication1.csproj" @@ -0,0 +1,14 @@ + + + + netcoreapp3.1 + + + + + + + + + + diff --git "a/\350\260\242\346\265\267\346\201\251/WebApplication1/appsettings.Development.json" "b/\350\260\242\346\265\267\346\201\251/WebApplication1/appsettings.Development.json" new file mode 100644 index 0000000..8983e0f --- /dev/null +++ "b/\350\260\242\346\265\267\346\201\251/WebApplication1/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git "a/\350\260\242\346\265\267\346\201\251/WebApplication1/appsettings.json" "b/\350\260\242\346\265\267\346\201\251/WebApplication1/appsettings.json" new file mode 100644 index 0000000..d9d9a9b --- /dev/null +++ "b/\350\260\242\346\265\267\346\201\251/WebApplication1/appsettings.json" @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} -- Gitee