【问题标题】:Access asp net core application connection string from another project从另一个项目访问 asp net core 应用程序连接字符串
【发布时间】:2023-03-16 12:13:01
【问题描述】:

我有一个 ASP NET CORE 项目,其中有一个连接字符串,而我的 dbcontext 位于 .net 4.52 类库项目中。我想知道如何从 asp net core 项目中访问连接字符串。

asp net core - appsettings.json:

"ConnectionStrings": {        
    "MyConnectionString": "Data Source=.\\mydb;Initial Catalog=mydb;User Id=myuser;Password=mypassword; MultipleActiveResultSets=true"
}, ...

我的dbContext

public MyContext() : base("name=MyConnectionString")
{
    Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>());
}

public MyContext(string connString) : base(connString)
{
    Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>());
}

当我运行应用程序时,我收到错误:在应用程序配置文件中找不到名为 MyConnectionString 的连接字符串。即使我在 asp net core 项目 web.config 文件中添加了一个连接字符串部分。

当我对连接字符串进行硬编码时,一切正常。

【问题讨论】:

    标签: .net entity-framework asp.net-core connection-string class-library


    【解决方案1】:

    我遇到了同样的问题,这让我很头疼。以下是我的解决方法:

    Visual Studio 问题

    首先,我使用 Visual Studios 2015 和 .Net Core 的预发布版本(dnx 之类的)创建了一个 .NET Core 网站。我一直在检查我的 appsettings.json 文件并试图弄清楚如何修复它,但无济于事。我所做的所有研究都说我使用的是旧版本的 .Net 核心框架,我需要升级。最后我厌倦了它并卸载了 VS15 并安装了 VS17,所以我会得到最新的最好的。

    错误的项目类型

    现在使用 VS17,我创建了一个全新的 .Net Core Web 应用程序并添加了我的 IO 类,结果却报错了

    "The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0."
    

    我收到该错误是因为我试图在 .Net Core 网站中使用 .Net 框架 OI 类。与其走这条路,我决定创建一个 .Net 框架 ASP.Net Core 网站。 (是的,有点混乱)

    mscorlib 的这个问题可能是 fixed soon?

    同样的老问题

    在我解决这个问题后,重新添加了我的 IO 类,然后将我的连接字符串添加到 appsettings.json 中,只是我遇到了完全相同的问题。

    InvalidOperationException: No connection string named 'myConnectionString' could be found in the application config file.
    

    在我把我的电脑扔出窗外之前,我注意到这次有一个 app.config 文件和 appsettings.json 文件。

    解决方案

    凭直觉,我将连接字符串从 appsettings.json 中取出并将其添加到 app.config 中,瞧!有效!我不确定这是否适用于 VS15 中的旧 DNX 框架,但如果您仍然遇到此问题,我会尝试添加 app.config。

    【讨论】:

    • 首先,感谢您在回复中付出的努力,您介意向我展示您的 app.config 吗?我已经尝试在我的 web.config 文件中放置一个连接字符串部分,但它不起作用。
    • 里面几乎没有任何东西。 &lt;configuration&gt; &lt;runtime&gt; &lt;gcServer enabled="true"/&gt; &lt;/runtime&gt; &lt;connectionStrings&gt; &lt;add name="myConnectionString" connectionString="metadata=res://*/Entity.RVModel.csdl|res://*/Entity.RVModel.ssdl|res://*/Entity.RVModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=&lt;myDatasource&gt;;initial catalog=&lt;database&gt;;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&amp;quot;" providerName="System.Data.EntityClient"/&gt; &lt;/connectionStrings&gt; &lt;/configuration&gt;
    • 如何在 ASP.NET 核心应用程序中拥有 web.config?当我创建一个网站时,我只获得用于配置应用程序的 .json 文件。
    • 这是一个 asp.net 核心 web api,当我创建任何 asp.net 核心应用程序时,它带有 web.config 而不是 app.config top one it's normal app, the bottom one is web api。也许这是 vs 2015 / vs 2017 的差异。我会尝试手动添加 app.config 并报告结果。
    猜你喜欢
    • 2011-09-09
    • 2020-01-16
    • 2014-01-26
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多