Create a DbContext that handle a DatabaseFactory to use DapperExtensions more easily

后端 未结 2 1750
走了就别回头了
走了就别回头了 2021-01-24 13:37

This days I try to create an abstract base repository using some basic CRUD functions proposed by DapperExtensions. But the code given as an exemple use a SqlConnection which is

2条回答
  •  余生分开走
    2021-01-24 14:16

    public abstract class ABaseRepository : IBaseRepository
            where M : BaseModel
        {
            private static DbProviderFactory factory = DbProviderFactories.GetFactory(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ProviderName);
            protected static DbConnection connection;
    
        public static IDbConnection CreateOpenConnection()
        {
            connection = factory.CreateConnection();
            connection.Open();
    
            return connection;
        }
    
        public dynamic Insert(M model)
        {
            dynamic multiKey;
            using (IDbConnection con = CreateOpenConnection())
            {
                multiKey = con.Insert(model);
            }
    
            return multiKey;
        }
     }
    

提交回复
热议问题