简体   繁体   English

我该如何解决这个错误? 参数 1:无法将“字符串”转换为“Microsoft.EntityFrameworkCore.DbContextOptions”

[英]How do I fix this error? Argument 1: cannot convert 'string' to 'Microsoft.EntityFrameworkCore.DbContextOptions'

I have a problem with DbContext .我对DbContext有疑问。 I decided to make a new class, which will be responsible for the connections with the Database.我决定创建一个新的 class,它将负责与数据库的连接。

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:但是,我在 base 名称附近有一个例外:

Argument 1: cannot convert 'string' to 'Microsoft.EntityFrameworkCore.DbContextOptions'.参数 1:无法将“字符串”转换为“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 .我缺少MembershipContextDBContextOptions的定义。 I just had to add it so that the method could recognize the connection string.我只需要添加它,以便该方法可以识别连接字符串。

Inside MembershipContext.cs :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))
        {

        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 参数 1:无法从“Microsoft.EntityFrameworkCore.DbContextOptions”转换为“字符串” - Argument 1: cannot convert from 'Microsoft.EntityFrameworkCore.DbContextOptions' to 'string' C# DbContext 问题:参数 1:无法将“字符串”转换为“Microsoft.EntityFrameworkCore.DbContextOptions” - C# Problem with DbContext: Argument 1: cannot convert 'string' to 'Microsoft.EntityFrameworkCore.DbContextOptions' 无法解析 Microsoft.EntityFrameworkCore.DbContextOptions 类型的服务 - Unable to resolve service for type Microsoft.EntityFrameworkCore.DbContextOptions 无法解析类型“Microsoft.EntityFrameworkCore.DbContextOptions”的服务 - Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions' 无法解析 Microsoft.EntityFrameworkCore.DbContextOptions .net6 类型的服务 - Unable to resolve service for type Microsoft.EntityFrameworkCore.DbContextOptions .net6 '在尝试激活时无法解析类型 ¨Microsoft.entityFrameworkCore.DbContextOptions¨1[LibraryData.LibraryContext] 的服务 - 'Unable to resolve service for type ¨Microsoft.entityFrameworkCore.DbContextOptions¨1[LibraryData.LibraryContext] while attempting to activate 如何解决该错误:参数1:无法从“字符串”转换为“ Name.Entity.Models.MakeModelInfo” - How do I fix the error: Argument 1: cannot convert from 'string' to 'Name.Entity.Models.MakeModelInfo 如何在不使用 LINQ 的情况下修复错误“参数 1:无法从“字符串”转换为“PassTask13_new.Customer” - How do I fix the error 'Argument 1: cannot convert from 'string' to 'PassTask13_new.Customer' without using LINQ 无法从“字符串”转换为“Microsoft.EntityFrameworkCore.ServerVersion” - Cannot convert from 'string' to 'Microsoft.EntityFrameworkCore.ServerVersion 如何修复错误参数3:c#中无法从'System.DateTime'转换为'Microsoft.Win32.Registry ValueOptions' - how to fix error Argument 3: cannot convert from 'System.DateTime' to 'Microsoft.Win32.Registry ValueOptions' in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM