ASP.NET: Why can I execute query once only?

Viewed 63

I have an ASP.NET service. Inside it I use the access to SQL Server, i.e. add the DbConnection etc

  services.AddScoped<IDbConnection, SqlConnection>(_ => new SqlConnection(sqlcon));
  services.AddScoped<ICRepository, CRepository>();

This DbConnection used in the classes which queried a db.

BUT I can execute the query 1st time and it returns the correct items. If I execute it the 2nd time I receive: "message": "The ConnectionString property has not been initialized."

and really the dbconnection object is empty and not initialized How to fix it? That is minimized piece of code

 public class CRepository : ICRepository
    {
        private readonly IDbConnection _db;

        public CRepository(IDbConnection db)
        {
            _db = db;
        }
//....
        public List<MyAction> GetMyActions()
        {
            var sql = $@"
                SELECT id,name, description from actions;";

            var result = _db.Query<MyAction>(sql).ToList();
            return result;
        }
...
}

ANd the first time the _db is initialized but the 2nd one - no.

0 Answers
Related