标签云

微信群

扫码加入我们

WeChat QR Code

I am using ASP.NET 5 / ASP.NET Core with EF7 / EF CoreI have the following repository class that uses ASP.NET 5's dependency injectionusing Microsoft.Data.Entity;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;namespace MyNamespace{public class PersonRepository :IPersonRepository{private MyDbContext _db;public MyRepository(MyDbContext db) {_db = db;}public async Task<IEnumerable<Person>> Search(){var table = from p in _db.Personsselect p;return await table.ToListAsync();}}public interface IPersonRepository{Task<IEnumerable<Person>> Search();}}Calling from my ASP.NET MVC controller I can call search successfully and get the data.public class PersonController : Controller{private IPersonRepository _personRepository;public PersonController (IPersonRepository personRepository){_personRepository= personRepository;}public async Task<IActionResult> PersonAction(){var result = await _personRepository.Search();}}However something odd happens when I call another repository method call of a stored proc in another repository before this working repositorycall.Stored procpublic async Task<List<sp_GetList_Result>> sp_GetList(){const string query = "exec [dbo].[sp_GetList];";var list = new List<sp_GetList_Result>();using (var connection = (SqlConnection)_db.Database.GetDbConnection()){using (var command = new SqlCommand(query, connection)){ connection.Open();using (var reader = await command.ExecuteReaderAsync()){while (reader.Read()){var row = new sp_GetList_Result();//populate rowlist.Add(row);}}connection.Close(); }}return list; }Action Method:public async Task<IActionResult> PersonAction(){var list = await _anotherRepository.GetList();var result = await _personRepository.Search();}Suddenlyawait _personRepository.Search(); throws an exceptionInnerException = {"The ConnectionString property has not been initialized."}I explicitly comment out connection.Close(); line but it still gives me the same connectionstring property has not been initialized error message.


ran into the same issue and i can't also explain it.

2019年04月22日28分33秒

you are my hero of the day!

2019年04月22日28分33秒