簡體   English   中英

在應用程序配置文件中找不到名為“”的連接字符串

[英]No connectionstring named '' could be found in the application config file

我知道這是之前被問過幾次的-答案基本相同。 但是在這些情況下,當更多項目正在使用相同的實體並且所有配置文件中都沒有連接字符串時,通常會出現此問題。 就我而言,所有項目都在同一個項目中。 並且連接字符串位於App.config中。 我在XAML中使用ViewModel:

<Window x:Class="ReportMapping.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ReportMapping"
        Title="Title" Height="477.22" Width="836.631">
    <Window.DataContext>
        <local:CompanyCollectionViewModel/>

    </Window.DataContext>...

我從ViewModel調用我的實體:

namespace ReportMapping
{
    class CompanyCollectionViewModel 
    {
        NAV_HelpersEntities dbContext = new NAV_HelpersEntities();

如果我在視圖模型可以正常工作的其他地方調用實體。 在示例中,從后面的代碼進行綁定。

連接字符串(如果在MainWindow.cs中使用,則可以使用):

<connectionStrings>
    <add name="NAV_HelpersEntities" connectionString="metadata=res://*/Mapping.csdl|res://*/Mapping.ssdl|res://*/Mapping.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=NAV\;initial catalog=NAV_Helpers;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

這里是NAV_HelpersEntities構造函數:

public partial class NAV_HelpersEntities : DbContext
    {
        public NAV_HelpersEntities()
            : base("name=NAV_HelpersEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<R_Component> R_Component { get; set; }
        public virtual DbSet<R_Configuration> R_Configuration { get; set; }
        public virtual DbSet<R_Masterdata> R_Masterdata { get; set; }
        public virtual DbSet<R_Type> R_Type { get; set; }
        public virtual DbSet<View_NAV_Entries> View_NAV_Entries { get; set; }
        public virtual DbSet<View_R_GetAccounts> View_R_GetAccounts { get; set; }
    }

當我調試CompanyCollectionViewModel構造函數時,我可以看到我的ObservableCollection正在被填充。

我在這里做錯了什么?

更新:好的,我現在通過將連接字符串添加到machine.config使其工作。 那行得通-但我想這只是我以后必須解決的變通辦法。 感謝您的輸入。

我已經找到了一個簡單的解決方案。 調用實體構造函數的基本構造函數時,必須從參數字符串中刪除“ name =“。 因此,您必須更改此設置:

public partial class SalesContext : DbContext 
{ 
    public SalesContext() 
        : base("name=SalesContext") 
    { 
    }

對此:

public partial  class SalesContext : DbContext 
{ 
    public SalesContext() 
        : base("SalesContext") 
    { 
    }

在此處查看: 實體框架MVVM遍歷1

更改此:

base("name=NAV_HelpersEntities")

對此:

base(nameOrConnectionString: "NAV_HelpersEntities")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM