Answered by:
moving to Ado.net core into ef core error InvalidOperationException: Multiple constructors accepting all given argument types have been found

Question
-
User-1355965324 posted
Currently I am using ado.net frame work with asp.net core. Now I am trying to move the database connection into entity frame work code. But it is not working and the following error is coming
InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'MyProject.GO.UI.Controllers.EmployeeController'. There should only be one applicable constructor.
I did the following but still is not working. Please any help would be very appreciated
Appsetting { "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } }, "ConnectionSetting": { "ConnectionString": "Data Source=MyDatabase;Initial Catalog=TestData; user id=sa; password=mypassword;" " }
In Startup.cs using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using MyProject.Common; using MyProject.GO.UI.Models; namespace MyProject.GO.UI { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext<AttendanceDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ConnectionString"))); //services.AddDbContextPool<AttendanceDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddSession(options => { options.IdleTimeout = TimeSpan.FromHours(5); }); services.AddMvc(); services.Configure<ConnectionSetting>(Configuration.GetSection("ConnectionSetting")); services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddOptions(); }
using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace MyProject.GO.UI.Models { public class AttendanceDbContext : DbContext { public AttendanceDbContext(DbContextOptions<AttendanceDbContext> options) : base(options) { } public DbSet<CustomerModel> Customer { get; set; } } }
Employee Controller using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.Extensions.Options; using MyProject.GO.Business; using MyProject.GO.Common; using MyProject.GO.UI.Models; using MyProject.GO.UI.Sevices; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data; using System.Globalization; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace MyProject.GO.UI.Controllers { public class EmployeeController : Controller { IOptions<ConnectionSetting> connectionSettings; private readonly AttendanceDbContext _dbcontext; public EmployeeController(AttendanceDbContext dbcontext) { _dbcontext = dbcontext; } // This is for ado.net public EmployeeController(IOptions<ConnectionSetting> connectionSettings) { this.connectionSettings = connectionSettings; }
Thursday, October 17, 2019 8:50 AM
Answers
-
User475983607 posted
Simply, you are not following ASP.NET Core standard DI patterns and practices. Use one constructor that includes all class dependencies.
namespace MyProject.GO.UI.Controllers { public class EmployeeController : Controller { private IOptions<ConnectionSetting> _connectionSettings; private readonly AttendanceDbContext _dbcontext; public EmployeeController(AttendanceDbContext dbcontext, IOptions<ConnectionSetting> connectionSettings) { _dbcontext = dbcontext; _connectionSettings = connectionSettings; }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, October 17, 2019 10:40 AM
All replies
-
User475983607 posted
Simply, you are not following ASP.NET Core standard DI patterns and practices. Use one constructor that includes all class dependencies.
namespace MyProject.GO.UI.Controllers { public class EmployeeController : Controller { private IOptions<ConnectionSetting> _connectionSettings; private readonly AttendanceDbContext _dbcontext; public EmployeeController(AttendanceDbContext dbcontext, IOptions<ConnectionSetting> connectionSettings) { _dbcontext = dbcontext; _connectionSettings = connectionSettings; }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, October 17, 2019 10:40 AM -
User-1355965324 posted
Many Thanks mgebhred.
After changing the code as you advised the following error is coming. Please can you help
System.ArgumentNullException: 'Value cannot be null.'
public void ConfigureServices(IServiceCollection services) { services.AddDbContext<AttendanceDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ConnectionString"))); //services.AddDbContextPool<AttendanceDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddSession(options => { options.IdleTimeout = TimeSpan.FromHours(5); }); services.AddMvc(); services.Configure<ConnectionSetting>(Configuration.GetSection("ConnectionSetting")); services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddOptions(); }
My Appsetting file is given below, { "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } }, "ConnectionSetting": { "ConnectionString": "MyData\\SQLEXPRESS;Initial Catalog=MyTable; user id=sa; password=pwd;" } }
Thursday, October 17, 2019 11:19 AM -
User475983607 posted
Once again, you are not following openly published standards! You removed the ConnectionStrings node but keep the standard Configuration.GetConnectionString which is looking for the missing node.
{ "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=GenericIdentity;Trusted_Connection=True;MultipleActiveResultSets=true" }, "Logging": { "LogLevel": { "Default": "Warning" } }, "AllowedHosts": "*" }
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer( Configuration.GetConnectionString("DefaultConnection")));
Thursday, October 17, 2019 11:31 AM -
User-1355965324 posted
Sorry for asking stupid question
I have to use the existing connection without any change for ado.net connection other wise the existing program will not work. In addition to that I want to create another connection for efcore enitity framework to using the same datatabase.
So How could I create new connection for the efcore entity to the same database like
Appsetting { "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } }, // I have to use this connection for ado.net "ConnectionSetting": { "ConnectionString": "Data Source=MyDatabase;Initial Catalog=TestData; user id=sa; password=mypassword;" " } In addition I want another connection { "ConnectionStrings": { "DefaultConnection": "Data Source=MyDatabase;Initial Catalog=TestData; user id=sa } }
Please can you advise me . I dont know I am asking stupid
Thursday, October 17, 2019 11:50 AM -
User475983607 posted
I have to use the existing connection without any change for ado.net connection other wise the existing program will not work. In addition to that I want to create another connection for efcore enitity framework to using the same datatabase.All you have to do is read the reference documentation for the library you are using and follow the patterns illustrate in the reference documentation.
"ConnectionStrings": { "DefaultConnection": "Data Source=MyDatabase;Initial Catalog=TestData; user id=sa", "AdoConnectionString": "Data Source=MyDatabase;Initial Catalog=TestData; user id=sa; password=mypassword;" },
Use DI in the controller or any class to get to the connection strings.
public class HomeController : Controller { public readonly IConfiguration Configuration; public HomeController(IConfiguration configuration) { Configuration = configuration; } [HttpGet] public IActionResult Index() { return Json(new { DefaultConnection = Configuration.GetConnectionString("DefaultConnection"), AdoConnectionString = Configuration.GetConnectionString("AdoConnectionString") }); }
Results
{ "defaultConnection": "Server=(localdb)\\mssqllocaldb;Database=GenericIdentity;Trusted_Connection=True;MultipleActiveResultSets=true", "adoConnectionString": "Data Source=MyDatabase;Initial Catalog=TestData; user id=sa; password=mypassword;" }
Thursday, October 17, 2019 1:30 PM