diff --git "a/\347\216\213\347\216\260\350\276\211/.gitignore" "b/\347\216\213\347\217\276\347\205\207/.gitignore" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/.gitignore" rename to "\347\216\213\347\217\276\347\205\207/.gitignore" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json.sln" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json.sln" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json.sln" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json.sln" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Controllers/TestController.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Controllers/TestController.cs" similarity index 42% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Controllers/TestController.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Controllers/TestController.cs" index bfc12e06da4d56d7e4294ba4d2fc3879847841fb..01403fc39a66425d5b83fcebf22a272926ce96e3 100644 --- "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Controllers/TestController.cs" +++ "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Controllers/TestController.cs" @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; @@ -15,17 +16,29 @@ namespace WebAPI__DI_json.Controllers [ApiController] public class TestController : ControllerBase { - private readonly IRespository _UserResitory; + private readonly IRespository _UserRespository; + private readonly IRespository _RoleRespository; - public TestController(IRespository respository) + public TestController(IRespository Urespository, IRespository Rrespository) { - _UserResitory = respository; + _UserRespository = Urespository; + _RoleRespository = Rrespository; } public string Get() { - var list = _UserResitory.Table.ToList(); - return JsonHelper.JsonSerializeSetting(list); + var users = _UserRespository.Table.ToList(); + + //var roles = _RoleRespository.Table.FirstOrDefault(); + + //_UserRespository.Insert(new User + //{ + // UserName = "www", + // PassWord = ".com", + // RoleId = roles.Ide + //}); + + return JsonHelper.JsonSerializeSetting(users); } } } diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Controllers/WeatherForecastController.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Controllers/WeatherForecastController.cs" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Controllers/WeatherForecastController.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Controllers/WeatherForecastController.cs" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/AdminDbContext.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/AdminDbContext.cs" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/AdminDbContext.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/AdminDbContext.cs" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/BaseEntity.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/BaseEntity.cs" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/BaseEntity.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/BaseEntity.cs" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/DbInitializer.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/DbInitializer.cs" similarity index 59% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/DbInitializer.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/DbInitializer.cs" index 9ccec366fbde0c12c565b82a0760bba0e2e437c4..82b543a356e81745fbec0dda25c2d06e5b9d4f0e 100644 --- "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/DbInitializer.cs" +++ "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/DbInitializer.cs" @@ -1,19 +1,25 @@ -using System; +using Microsoft.Extensions.DependencyInjection; +using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices.ComTypes; using System.Threading.Tasks; using WebAPI__DI_json.DoMain.Entity; +using WebAPI__DI_json.Implementation; +using WebAPI__DI_json.Interface; namespace WebAPI__DI_json.DoMain { public class DbInitializer { - public static void Seed() + public static void Seed(IServiceProvider serviceProvider) { - var db = new AdminDbContext(); - using (db) + using (var scope = serviceProvider.CreateScope()) { + var db = scope.ServiceProvider.GetService(typeof(AdminDbContext)) as AdminDbContext;//强制轉換類型 + var _userRespository = scope.ServiceProvider.GetService(typeof(IRespository)) as EfRespository; + var _RoleRespository = scope.ServiceProvider.GetService(typeof(IRespository)) as EfRespository; + db.Database.EnsureCreated(); var HasUser = db.Users.Any(); @@ -25,10 +31,9 @@ namespace WebAPI__DI_json.DoMain Description = "此处省略一百字" }; - db.Roles.Add(role); - db.SaveChanges(); + _RoleRespository.Insert(role); - db.Users.AddRange(new User[] + _userRespository.InsertBulk(new User[] { new User { @@ -45,13 +50,14 @@ namespace WebAPI__DI_json.DoMain } }); - db.SaveChanges(); + } + } + //var db = new AdminDbContext(); - } } } } diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/Role.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/Role.cs" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/Role.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/Role.cs" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/User.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/User.cs" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/User.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/User.cs" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Helper/JsonHelper.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Helper/JsonHelper.cs" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Helper/JsonHelper.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Helper/JsonHelper.cs" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Implementation/EfRespository.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Implementation/EfRespository.cs" similarity index 74% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Implementation/EfRespository.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Implementation/EfRespository.cs" index f6dc5cd24b2a603b3ee4e597fb61e0944f9fbb91..39f4474af867782592a9cc9442afe07c5f32b4c2 100644 --- "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Implementation/EfRespository.cs" +++ "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Implementation/EfRespository.cs" @@ -18,9 +18,9 @@ namespace WebAPI__DI_json.Implementation db = dbContext; } - public DbSet _entity; + private DbSet _entity; - public IQueryable Entity + protected DbSet Entity { get { @@ -44,28 +44,28 @@ namespace WebAPI__DI_json.Implementation public T GetById(int Id) { - return _entity.Where(x => x.Id == Id).FirstOrDefault(); + return Table.Where(x => x.Id == Id).FirstOrDefault(); } //删 - public void Delete(T Entity) + public void Delete(T entity) { - this._entity.Remove(Entity); + Entity.Remove(entity); db.SaveChanges(); } public void Delete(int Id) { - var Del = Entity.Where(x => x.Id == Id).FirstOrDefault(); + var Del = Table.Where(x => x.Id == Id).FirstOrDefault(); Delete(Del); } //增 - public void Insert(T Entity) + public void Insert(T entity) { - _entity.Add(Entity); + Entity.Add(entity); db.SaveChanges(); } @@ -73,14 +73,14 @@ namespace WebAPI__DI_json.Implementation public void InsertBulk(IEnumerable list) { - _entity.AddRange(list); + Entity.AddRange(list); db.SaveChanges(); } //改 - public void Update(T Entity) + public void Update(T entity) { - _entity.Update(Entity); + Entity.Update(entity); db.SaveChanges(); } } diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Interface/IRespository.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Interface/IRespository.cs" similarity index 78% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Interface/IRespository.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Interface/IRespository.cs" index b53d50ef499baa24ae8053c452a49ad3a494687b..f9a5e60c694596660d3f9d8a779481ba6f7eedb0 100644 --- "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Interface/IRespository.cs" +++ "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Interface/IRespository.cs" @@ -11,13 +11,13 @@ namespace WebAPI__DI_json.Interface IQueryable Table { get; } - void Insert(T Entity); + void Insert(T entity); void InsertBulk(IEnumerable list); - void Update(T Entity); + void Update(T entity); - void Delete(T Entity); + void Delete(T entity); void Delete(int Id); diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Program.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Program.cs" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Program.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Program.cs" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Properties/launchSettings.json" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Properties/launchSettings.json" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Properties/launchSettings.json" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Properties/launchSettings.json" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Startup.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Startup.cs" similarity index 93% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Startup.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Startup.cs" index 61f5072830528f1571a7c4f25f7a0ff711ce3dc6..951d7edb537ce4736645933a15ae2c5201af529e 100644 --- "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Startup.cs" +++ "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Startup.cs" @@ -28,7 +28,7 @@ namespace WebAPI__DI_json public void ConfigureServices(IServiceCollection services) { services.AddDbContext(); - services.AddScoped(typeof(IRespository<>), typeof(EfRespository<>)); + services.AddScoped(typeof(IRespository<>), typeof(EfRespository<>));//עķҲһN services.AddControllers(); } @@ -49,7 +49,7 @@ namespace WebAPI__DI_json endpoints.MapControllers(); }); - DbInitializer.Seed(); + DbInitializer.Seed(app.ApplicationServices);//һӿ } } } diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/WeatherForecast.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/WeatherForecast.cs" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/WeatherForecast.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/WeatherForecast.cs" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/WebAPI\342\200\224\342\200\224DI_json.csproj" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/WebAPI\342\200\224\342\200\224DI_json.csproj" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/WebAPI\342\200\224\342\200\224DI_json.csproj" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/WebAPI\342\200\224\342\200\224DI_json.csproj" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/appsettings.Development.json" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/appsettings.Development.json" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/appsettings.Development.json" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/appsettings.Development.json" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/appsettings.json" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/appsettings.json" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/appsettings.json" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/appsettings.json"