I'm new to .NET Core. While I'm connecting to a SQL Server database, I'm getting an error:
Unable to resolve service for type 'MVC_Core.Business.Repo' while attempting to activate 'MVC_Core.Controllers.AbcController
My StartUp.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase")));
services.AddTransient<IRepo,Repo>();
}
Application.js:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
},
"ConnectionStrings": {
"BloggingDatabase": "Data Source=MD\\MD;Initial Catalog=Ems_local;User ID=sa;Password=123"
}
},
"AllowedHosts": "*"
}
My DbContext:
public class ConnectToDb : DbContext
{
//public DbConnection(){}
public ConnectToDb(DbContextOptions<ConnectToDb> options) : base(options)
{
}
public virtual DbSet<Country> Country { get; set; }
}
This connection I'm calling like this:
public class Repo : IRepo
{
private ConnectToDb db = null;
public Repo(ConnectToDb _db)
{
db = _db;
}
While I'm calling this in my controller as
Repo ObjRepo;
public AbcController(Repo _objRepo)
{
ObjRepo = _objRepo;
}
[Route("Hello")]
public IActionResult Index()
{
var x = ObjRepo.GetCountry();
return Json("abc" + x);
}
Please guide me - why am I getting this error?