标签:etl 版本 引用 instance exist ret 12px attribute 数据
SqlSugar 是一款简单易用的ORM ,在国内市场占有率也比较高,
在今年10月份官网改版后 提供了完整的服务,让您的项目没有后顾之忧
下载地址 :https://github.com/sunkaixuan/SqlSugar
打开VS2019及以上版本 ,然后新建一个Mvc项目,默认MVC是不支持路由的,需要在Startup.cs里面加入
app.UseEndpoints(endpoints => { endpoints.MapControllerRoute (name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); endpoints.MapControllerRoute ("areaRoute", "{area:exists}/{controller=Admin}/{action=Index}/{id?}"); });
然后删除自带的Pages
在根目录创建Controllers文件夹和Views文件夹 ,创建HomerController.cs和 Index.cshtml
自带的IOC使用非常简单 ,我们这以Auface ioc为例子讲解
Autofac
Autofac.Extensions.DependencyInjection
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseServiceProviderFactory(new AutofacServiceProviderFactory())//添加这一行 .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }
public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); services.AddControllers().AddControllersAsServices();//-----------------添加这一行 } public void ConfigureContainer(ContainerBuilder builder) //----------------添加这个方法 { builder.RegisterType<OrderDal>().InstancePerLifetimeScope(); var webAssemblytype = typeof(Program).Assembly; builder.RegisterAssemblyTypes(webAssemblytype).PropertiesAutowired(); }
public class HomeController : Controller { public OrderDal order { get; set; }//定义他就能注入了 public IActionResult Index() { var list = order.GetList(); return View(); } }
nuget只需要引用一个dll文件,
.net 版本选择 sqlSugar
.net core版本选择 sqlSugarCor
public class Repository<T> : SimpleClient<T> where T : class, new() { public Repository(ISqlSugarClient context = null) : base(context) { if (context == null) { base.Context = new SqlSugarClient(new ConnectionConfig() { DbType = SqlSugar.DbType.SqlServer, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true, ConnectionString = Config.ConnectionString }); } } /// <summary> /// 扩展方法,自带方法不能满足的时候可以添加新方法 /// </summary> /// <returns></returns> public List<T> GetDeleteList() { return Context.Queryable<T>().Where(" isdeleted=1 ").ToList(); } }
就这样OrderDal就编写完成了
public class OrderDal: Repository<Order> { //创建OrderItem public Repository<OrderItem> OrderItem => new Repository<OrderItem>(base.Context); public List<OrderItem> GetOrderItems() { return OrderItem.GetList(); //使用OrderItem } public List<Order> GetOrders() { return base.GetList(); //使用自已的仓储方法 } }
上面简简单单几行就完成了一个IOC+仓储的 例子
SqlSugar仓储自带的方法有很多 ,基本常用开发,不能满足的在 Repository中添加仓储方法
var data1 = base.GetById(1); var data2 = base.GetList(); var data3 = base.GetList(it => it.Id == 1); var data4 = base.GetSingle(it => it.Id == 1); var p = new PageModel() { PageIndex = 1, PageSize = 2 }; var data5 = base.GetPageList(it => it.Name == "xx", p); Console.Write(p.PageCount); var data6 = base.GetPageList(it => it.Name == "xx", p, it => it.Name, OrderByType.Asc); Console.Write(p.PageCount); List<IConditionalModel> conModels = new List<IConditionalModel>(); conModels.Add(new ConditionalModel(){FieldName="id",ConditionalType=ConditionalType.Equal,FieldValue="1"});//id=1 var data7 = base.GetPageList(conModels, p, it => it.Name, OrderByType.Asc); base.AsQueryable().Where(x => x.Id == 1).ToList(); //插入 base.Insert(insertObj); base.InsertRange(InsertObjs); var id = base.InsertReturnIdentity(insertObj); base.AsInsertable(insertObj).ExecuteCommand(); //删除 base.Delete(insertObj); base.DeleteById(1); base.DeleteById(new int[] { 1, 2 }); base.Delete(it => it.Id == 1); base.AsDeleteable().Where(it => it.Id == 1).ExecuteCommand(); //更新 base.Update(insertObj); base.UpdateRange(InsertObjs); base.Update(it => new Order() { Name = "a", }, it => it.Id == 1); base.AsUpdateable(insertObj).UpdateColumns(it=>new { it.Name }).ExecuteCommand();
上面使用是Auface 实现的MVC,如果我们要使用自带的MVC怎么办?
1、配置IOC,非常简单 就几行代码
public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); services.AddScoped<OrderDal>();//添加这2行 services.AddDirectoryBrowser();//添加这2行 }
2、使用IOC ,通过构造函数进行注入
public class HomeController : Controller { public OrderDal order { get; set; } public HomeController(OrderDal order) { this.order = order; } public IActionResult Index() { var list = order.GetList(); return View(); } }
https://github.com/sunkaixuan/SqlSugar
.Net Core 3.0 MVC 中使用 SqlSugar ORM框架
标签:etl 版本 引用 instance exist ret 12px attribute 数据
原文地址:https://www.cnblogs.com/sunkaixuan/p/13853560.html