diff --git "a/\351\273\204\347\204\225/DITest.sln" "b/\351\273\204\347\204\225/DITest.sln" new file mode 100644 index 0000000000000000000000000000000000000000..b81742a2dcddc7c0c05b6fb8b49c6fa279e31e70 --- /dev/null +++ "b/\351\273\204\347\204\225/DITest.sln" @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.757 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DITest", "DITest\DITest.csproj", "{9E7F3175-7DB2-4556-B56F-6C99FFB05F9E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9E7F3175-7DB2-4556-B56F-6C99FFB05F9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9E7F3175-7DB2-4556-B56F-6C99FFB05F9E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9E7F3175-7DB2-4556-B56F-6C99FFB05F9E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9E7F3175-7DB2-4556-B56F-6C99FFB05F9E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B56D6639-90CA-4B06-8271-A35FE9FB5F3B} + EndGlobalSection +EndGlobal diff --git "a/\351\273\204\347\204\225/DITest/Controllers/ValuesController.cs" "b/\351\273\204\347\204\225/DITest/Controllers/ValuesController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..d9ce69d39d430aea0219f4d07c68c24293c9160a --- /dev/null +++ "b/\351\273\204\347\204\225/DITest/Controllers/ValuesController.cs" @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using DITest.Damain; +using DITest.Damain.Entity; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace DITest.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class ValuesController : ControllerBase + { + + + private readonly Admin1100DbContext _db; + + public ValuesController(Admin1100DbContext dbContext) + { + _db = dbContext; + + } + + + // GET api/values + [HttpGet] + public string Get() + { + var res = _db.Users.ToList(); + + + + var x = JsonConvert.SerializeObject(res, Formatting.Indented, new JsonSerializerSettings + { + ReferenceLoopHandling = ReferenceLoopHandling.Ignore, + DateFormatString = "yyyy-MM-dd HH:mm:ss" + }); + + + return x; + + //var list = new List(); + + //using (var db = new Admin1100DbContext()) + //{ + // list = db.Users.ToList(); + //} + + //return list; + } + + // GET api/values/5 + //[HttpGet("{id}")] + //public ActionResult Get(int id) + //{ + + //} + + //// POST api/values + //[HttpPost] + //public void Post([FromBody] string value) + //{ + //} + + //// PUT api/values/5 + //[HttpPut("{id}")] + //public void Put(int id, [FromBody] string value) + //{ + //} + + //// DELETE api/values/5 + //[HttpDelete("{id}")] + //public void Delete(int id) + //{ + //} + } +} diff --git "a/\351\273\204\347\204\225/DITest/DITest.csproj" "b/\351\273\204\347\204\225/DITest/DITest.csproj" new file mode 100644 index 0000000000000000000000000000000000000000..5da714e787d855b8376aad3ed3ce3b71f21b6929 --- /dev/null +++ "b/\351\273\204\347\204\225/DITest/DITest.csproj" @@ -0,0 +1,18 @@ + + + + netcoreapp2.1 + + + + + + + + + + + + + + diff --git "a/\351\273\204\347\204\225/DITest/Domain/Admin1100DbContext.cs" "b/\351\273\204\347\204\225/DITest/Domain/Admin1100DbContext.cs" new file mode 100644 index 0000000000000000000000000000000000000000..55ddcd2d823f1a5edadd0075ba12ea46d8bae43a --- /dev/null +++ "b/\351\273\204\347\204\225/DITest/Domain/Admin1100DbContext.cs" @@ -0,0 +1,29 @@ +using DITest.Damain.Entity; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DITest.Damain +{ + public class Admin1100DbContext:DbContext + { + public Admin1100DbContext() + { + + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + var connectionString = "server=.;database=Asmin1100;uid=sa;pwd=123456"; + + optionsBuilder.UseSqlServer(connectionString); + } + + public DbSet Users { get; set; } + + public DbSet Roles { get; set; } + + } +} diff --git "a/\351\273\204\347\204\225/DITest/Domain/BaseEntity.cs" "b/\351\273\204\347\204\225/DITest/Domain/BaseEntity.cs" new file mode 100644 index 0000000000000000000000000000000000000000..0a778bc29938e45454dd22f3c23b37d2e2e97e55 --- /dev/null +++ "b/\351\273\204\347\204\225/DITest/Domain/BaseEntity.cs" @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DITest.Damain +{ + public abstract 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; } + + } +} diff --git "a/\351\273\204\347\204\225/DITest/Domain/DbInitializer.cs" "b/\351\273\204\347\204\225/DITest/Domain/DbInitializer.cs" new file mode 100644 index 0000000000000000000000000000000000000000..23b1e268cba44b9e5e833fd6f57dadbff2db7b0c --- /dev/null +++ "b/\351\273\204\347\204\225/DITest/Domain/DbInitializer.cs" @@ -0,0 +1,49 @@ +using DITest.Damain.Entity; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DITest.Damain +{ + public class DbInitializer + { + + public static void Seed() + { + using (Admin1100DbContext db = new Admin1100DbContext()) + { + db.Database.EnsureCreated(); + + var hasUser = db.Users.Any(); + + + if (!hasUser) + { + var role = new Roles + { + RoleName = "学生", + Description = "这是一个学生职业" + + }; + db.Roles.Add(role); + + db.SaveChanges(); + + + db.Users.Add(new Users + { + UserName = "小明", + Password = "hao123", + RolesId = role.Id + }); + + db.SaveChanges(); + + } + } + + + } + } +} diff --git "a/\351\273\204\347\204\225/DITest/Domain/Entity/Roles.cs" "b/\351\273\204\347\204\225/DITest/Domain/Entity/Roles.cs" new file mode 100644 index 0000000000000000000000000000000000000000..275a8c3b492907477a9cf3591b034e3ecabf6156 --- /dev/null +++ "b/\351\273\204\347\204\225/DITest/Domain/Entity/Roles.cs" @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DITest.Damain.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/\351\273\204\347\204\225/DITest/Domain/Entity/Users.cs" "b/\351\273\204\347\204\225/DITest/Domain/Entity/Users.cs" new file mode 100644 index 0000000000000000000000000000000000000000..3a8e59cd37c3036e7a738278339b37c2cfc1bc9b --- /dev/null +++ "b/\351\273\204\347\204\225/DITest/Domain/Entity/Users.cs" @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DITest.Damain.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/\351\273\204\347\204\225/DITest/Program.cs" "b/\351\273\204\347\204\225/DITest/Program.cs" new file mode 100644 index 0000000000000000000000000000000000000000..8b0345120c423caa3d7a36cc0e4da7082b2d0f0b --- /dev/null +++ "b/\351\273\204\347\204\225/DITest/Program.cs" @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; + +namespace DITest +{ + public class Program + { + public static void Main(string[] args) + { + CreateWebHostBuilder(args).Build().Run(); + } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + WebHost.CreateDefaultBuilder(args) + .UseStartup(); + } +} diff --git "a/\351\273\204\347\204\225/DITest/Properties/launchSettings.json" "b/\351\273\204\347\204\225/DITest/Properties/launchSettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..9407d2b757587ad41cb297945ab52491834145c6 --- /dev/null +++ "b/\351\273\204\347\204\225/DITest/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:2290", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "api/values", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "DITest": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "api/values", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} \ No newline at end of file diff --git "a/\351\273\204\347\204\225/DITest/Startup.cs" "b/\351\273\204\347\204\225/DITest/Startup.cs" new file mode 100644 index 0000000000000000000000000000000000000000..29dc52ffca8c7a0ba027b38c8c65ebb6f76ed3fe --- /dev/null +++ "b/\351\273\204\347\204\225/DITest/Startup.cs" @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using DITest.Damain; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace DITest +{ + 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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IHostingEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseMvc(); + + + DbInitializer.Seed(); + } + } +} diff --git "a/\351\273\204\347\204\225/DITest/appsettings.Development.json" "b/\351\273\204\347\204\225/DITest/appsettings.Development.json" new file mode 100644 index 0000000000000000000000000000000000000000..e203e9407e74a6b9662aab8fde5d73ae64665f18 --- /dev/null +++ "b/\351\273\204\347\204\225/DITest/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git "a/\351\273\204\347\204\225/DITest/appsettings.json" "b/\351\273\204\347\204\225/DITest/appsettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..def9159a7d9403c04a926f64e71ef3ee7c9e4c57 --- /dev/null +++ "b/\351\273\204\347\204\225/DITest/appsettings.json" @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Warning" + } + }, + "AllowedHosts": "*" +}