【问题标题】:Why is my connection string not found, when I am referencing the project where it exists [duplicate]当我引用它存在的项目时,为什么找不到我的连接字符串[重复]
【发布时间】:2017-11-04 05:06:11
【问题描述】:

我已经构建了一个单独的数据层,其中包含我的实体框架。我在当前项目中引用了这个单独的项目...我将以下内容添加到当前的 MVC 控制器中:

using SMCD_DataLayer;

  [HttpPost]
        public ActionResult Login(LoginViewModel lv)
        {
            var db = new SMCD_DataStoreEntities();
            var found = db.Users.Select(x => x.Username == lv.userId && x.Password == lv.passWord).Any();

            if (ModelState.IsValid)
            {
                if (!found)
                {
                    lv.errorMsg = "User ID is invalid!";
                }
                else
                {
                    return RedirectToAction("IntroPage", "Devices");
                }
            }

            return View(lv);
        }

我的 SMCD_DataStoreEntities 位于 SMCD_DataLayer 中,但此控制器位于 SMCD_Portal 项目中。

就行 var found ... 我收到以下错误消息:

发生 System.InvalidOperationException H结果=0x80131509 Message=在应用程序配置文件中找不到名为“SMCD_DataStoreEntities”的连接字符串。

连接字符串位于 SMCD_Datalayer 中,即使我在当前项目中引用它,它是否需要位于 SMCD_Portal 项目中?

这是位于 SMCD_DataLayer 的 App.config 中的连接字符串,记住我的控制器位于 SMCD_Portal 中

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <connectionStrings>
    <add name="SMCD_DataStoreEntities" connectionString="metadata=&quot;res://*/SMCD DataStore.csdl|res://*/SMCD DataStore.ssdl|res://*/SMCD DataStore.msl&quot;;provider=System.Data.SqlClient;provider connection string=&quot;data source=KARL-LT\SQLEXPRESS;initial catalog=SMCD_DataStore;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

【问题讨论】:

  • 请添加相关代码。您当前提供的代码均未显示连接字符串。您也没有从 web.config(或相关资源)提供连接字符串。 去掉实际的连接字符串信息,我们不需要知道主机、数据库名、用户名或密码
  • 我可以给你看
  • 我的主要问题是......实体框架可以存在于另一个项目中还是必须存在于我的控制器正在使用它的当前项目中?
  • 你的问题在任何地方听起来都不像那样。但直接答案是否定的,如果解决方案和项目布局正确,您的 MVC 应用程序不必有任何对 EF 的引用。 (除了 web.config)
  • 但是,您的连接字符串需要位于 MVC 应用程序的 .config 文件中。在类库的配置文件中包含它是行不通的。

标签: c# asp.net-mvc


【解决方案1】:

您的应用程序使用或任何引用程序集使用的任何 .config 设置通常都需要位于主应用程序的 .config 文件中。

这同样适用于连接字符串。

因此,将这些连接字符串放在主应用程序的 .config 文件中,就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    相关资源
    最近更新 更多