【问题标题】:DotNet Core - Connection String from Class LibraryDotNet Core - 类库中的连接字符串
【发布时间】:2018-11-13 21:23:25
【问题描述】:

我的 SQL 连接字符串存储在 appsettings.json 中的 Web 项目中

  "ConnectionStrings": {
    "MyDbConnectionString": "***"
  },

然后我使用 Scaffold 添加了一个数据库上下文

Scaffold-DbContext -Connection "name=MyDbConnectionString" -Provider "Microsoft.EntityFrameworkCore.SqlServer"  ...  -Force

我可以在控制器中使用上下文,并且在获取数据或写入方面没有问题。但是,我希望我所有的业务逻辑都在一个单独的类库上。所以这是我的图书馆中的存储库:

    public class MyRepository
{

    private static MyContext CurrentContext
    {
        get { return new MyContext(); }
    }

    public static async void AddEventLog(EventLog eventLog)
    {
        using (var context = CurrentContext)
        {
            context.EventLog.Add(eventLog);
            await context.SaveChangesAsync();
        }
    }
}

但它在尝试写入数据库时​​失败。

System.InvalidOperationException: 'A named connection string was used, but the name 'MyDbConnectionString' was not found in the application's configuration.

我是否应该将 appsettings.json 添加到库项目中(这似乎是多余的,不正确的)?我错过了什么?如何引用回 Web 项目 appsettings.json 文件?

任何帮助将不胜感激。

这是我的创业公司

services.AddDbContext<MyContext>(options =>options.UseSqlServer(Configuration.GetConnectionString("MyDbConnectionString")));

【问题讨论】:

    标签: .net-core


    【解决方案1】:

    ***** 这里是我对工作所做的更改 *****

    我发现了我认为的问题,所以我们开始吧。

    1. 从 MySsdCaseContext 中删除以下内容。

      public MySsdCaseContext()
      {
      }
      

      并保留这个..

       public MySsdCaseContext(DbContextOptions<MySsdCaseContext> options) : base(options)
      {
      }
      
    2. 为了修复此注释,请从 OnConfiguring 中删除以下内容。

      if (!optionsBuilder.IsConfigured)
          {
              optionsBuilder.UseSqlServer("name=MySsdCaseDb");
          }
      
    3. 在 startup.cs 中的 ConfigureService 方法中添加以下内容。

      services.AddDbContext<MySsdCaseContext>(options 
      =>options.UseSqlServer(Configuration.GetConnectionString("MySsdCaseDb")));
      

    这应该会提示您添加对 MySsdCase.Core.Data 类库的引用。你目前没有这个。基本上放 startup.cs 顶部的以下内容

        using MySsdCase.Core.Data;
    
    1. 确保以下内容位于 MySsdCase.Web.cspoj 中

      <ItemGroup>
         <ProjectReference Include="..\MySsdCase.Core\MySsdCase.Core.csproj" />
      </ItemGroup>
      
    2. 这样做...

            public class EventLogRepository 
          {
      private readonly MySsdCaseContext _context;
      
      public async Task AddEventLogAsync(EventLog eventLog)
      {
      
          var myVar = await _context.Set<ClientDetails>()
              .AsNoTracking()
              .Select(p => p)
              .Take(2)
              .ToListAsync();
      }
      }
      

    我认为总体而言,startup.cs 中的 BL 中没有对 DAL 的引用。

    【讨论】:

    • 我在启动时有 AddDbContext,但我没有在我的问题中发布。我编辑了我的帖子并添加了。但是,您建议使用 AddDbContextPool,我尝试使用它,但仍然遇到相同的错误。顺便说一句.. 连接字符串正在工作,因为我可以从控制器中使用它。错误只是当我从类库中使用它时。
    • 我一直在努力解决这个问题......我相信我已经缩小了我正在寻找的范围,即如何将连接字符串通过 DI 传递到我的存储库。我已在此链接上尝试过答案:stackoverflow.com/questions/43058497/… 但我仍然遇到问题。
    • 没关系...在索引中我没有在此调用中传递上下文:_eventRepo = new EventLogRepository(context); @Bazza 我非常感谢您的帮助!再次感谢!
    猜你喜欢
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    相关资源
    最近更新 更多