moving to Ado.net core into ef core error InvalidOperationException: Multiple constructors accepting all given argument types have been found [Answered]RSS
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
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;
}
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.
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
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")
});
}
Member
410 Points
1326 Posts
moving to Ado.net core into ef core error InvalidOperationException: Multiple constructors accep...
Oct 17, 2019 08:50 AM|polachan|LINK
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
All-Star
53001 Points
23593 Posts
Re: moving to Ado.net core into ef core error InvalidOperationException: Multiple constructors a...
Oct 17, 2019 10:40 AM|mgebhard|LINK
Simply, you are not following ASP.NET Core standard DI patterns and practices. Use one constructor that includes all class dependencies.
Member
410 Points
1326 Posts
Re: moving to Ado.net core into ef core error InvalidOperationException: Multiple constructors a...
Oct 17, 2019 11:19 AM|polachan|LINK
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(); }
All-Star
53001 Points
23593 Posts
Re: moving to Ado.net core into ef core error InvalidOperationException: Multiple constructors a...
Oct 17, 2019 11:31 AM|mgebhard|LINK
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.
Member
410 Points
1326 Posts
Re: moving to Ado.net core into ef core error InvalidOperationException: Multiple constructors a...
Oct 17, 2019 11:50 AM|polachan|LINK
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
Please can you advise me . I dont know I am asking stupid
All-Star
53001 Points
23593 Posts
Re: moving to Ado.net core into ef core error InvalidOperationException: Multiple constructors a...
Oct 17, 2019 01:30 PM|mgebhard|LINK
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.
Use DI in the controller or any class to get to the connection strings.
Results