I have a problem with DbContext
. I decided to make a new class, which will be responsible for the connections with the Database.
public class DataContextService : IDataContextService
{
private IMediator _mediator { get; }
private CommonContext _commonContext { get; set; }
private MembershipContext _membershipContext { get; set; }
private LogsContext _logContext { get; set; }
private MongoContext _mongoContext { get; set; }
private IServiceScopeFactory _scopeService;
However, I have an exception near the name of base:
Argument 1: cannot convert 'string' to 'Microsoft.EntityFrameworkCore.DbContextOptions'.
I found a similar issue and response with code similar to mine, but I am not sure how to fix this issue. I would appreciate your help.
internal MembershipContext Membership_Context
{
get
{
if (_membershipContext == null)
{
//This is where the error happens
_membershipContext = new MembershipContext(AwsSecretsManager.Get(AwsSecretNames.DbsMembership, "BusinessObjects"));
}
return _membershipContext;
}
}
I found how to fix the error. I was missing the definition for DBContextOptions
inside the MembershipContext
. I just had to add it so that the method could recognize the connection string.
Inside MembershipContext.cs
:
private static DbContextOptions GetOptions(string connectionString)
{
return SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), connectionString, o => o.CommandTimeout(300)).Options;
}
public MembershipContext(string connectionString) : base(GetOptions(connectionString))
{
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.