/// <summary> /// 使用IDatabase用法 /// </summary> /// <param name="services"></param> /// <param name="sqlDialect"></param> /// <param name="CreateConnection"></param> /// <param name="UseExtension">是否同时使用扩展方法</param> /// <returns></returns> public static IServiceCollection AddDapperDataBase(this IServiceCollection services, ESqlDialect sqlDialect, Func <IDbConnection> CreateConnection, bool UseExtension = false) { var SqlDialect = SqlDialectUtil.ConvertESqlDialect(sqlDialect); services.AddOptions(); services.Configure <DataBaseOptions>(opt => { opt.DbConnection = CreateConnection; opt.sqlDialect = SqlDialectUtil.ConvertESqlDialect(sqlDialect); }); if (UseExtension) { var Configuration = new DapperExtensionsConfiguration(SqlDialect); DapperExtensions.Configure(Configuration); services.AddSingleton <IDapperExtensionsConfiguration>(Configuration); } else { services.AddSingleton <IDapperExtensionsConfiguration, DapperExtensionsConfiguration>(); } services.AddTransient <IDatabase, Database>(); return(services); }
protected IClassMapper GetClassMapper <T>() where T : class { IClassMapper map = DapperExtensions.GetMap <T>(); if (map == null) { throw new NullReferenceException(string.Format("Map was not found for {0}", typeof(T))); } return(map); }
protected string GetColumnName <T>(string propertyName) where T : class { IClassMapper map = DapperExtensions.GetMap <T>(); if (map == null) { throw new NullReferenceException(string.Format("Map was not found for {0}", typeof(T))); } IPropertyMap propertyMap = map.Properties.Single(p => p.Name == propertyName); if (map == null) { throw new NullReferenceException(string.Format("{0} was not found for {1}", propertyName, typeof(T))); } return(DapperExtensions.SqlGenerator.GetColumnName(map, propertyMap, false)); }
/// <summary> /// 设置SqlDialect,使用默认用法 /// </summary> /// <param name="services"></param> /// <param name="SqlDialect"></param> /// <example> /// using(var con=new SqlConnection(Configuration.GetConnectionString("DefaultConnection")) /// { /// con.Insert<TestData>(data); /// } /// </example> /// <returns></returns> public static IServiceCollection AddDapper(this IServiceCollection services, ESqlDialect SqlDialect) { DapperExtensions.Configure(SqlDialectUtil.ConvertESqlDialect(SqlDialect)); return(services); }