asp.net Core2.0 + Dapper 简单案例

第一步肯定是先安装Dapper

第二步贴代码

    public class DataBaseConfig: IDataBaseConfig
    {
        private static string DefaultSqlConnectionString = @"Data Source=.;Initial Catalog=NetCoreData;User ID=sa;Password=123456;MultipleActiveResultSets=true";
        public IDbConnection GetSqlConnection(string sqlConnectionString = null)
        {
            if (string.IsNullOrWhiteSpace(sqlConnectionString))
            {
                sqlConnectionString = DefaultSqlConnectionString;
            }
            IDbConnection conn = new SqlConnection(sqlConnectionString);
            conn.Open();
            return conn;
        }
    }

第三步 注入

        public void ConfigureServices(IServiceCollection services)
        {
            //泛型注入
            //services.AddSingleton(typeof(IRepositoryBase<>), typeof(RepositoryBase<>));
//测试 services.AddSingleton<IDataBaseConfig, DataBaseConfig>(); services.AddMvc(); }

第四步 测试

由于第一次使用Dapper, 不足之处还希望大佬们指出

猜你喜欢

转载自www.cnblogs.com/MingQiu/p/9177610.html