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

c# entity-framework-6 wpf

Question

I know this is asked several times before - and with mostly the same answer. But in these cases the problem often occours when more projects are using same entity and connectionstring not being present in all config files. In my case all is in the same project. And the connection string is present in App.config. I am using a viewmodel in XAML:

<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>...

I am calling my entity from my ViewModel:

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

If I call the entity form other places that my viewmodel it works. In example binding from code behind.

The connectionstring (which is working if used in 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>

And here the NAV_HelpersEntities constructor:

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; }
    }

when I debug the CompanyCollectionViewModel constructor I can see that my ObservableCollection is being populated.

What am I doing wrong here?

UPDATE: Allright I got it working for now by adding the connectionstring to machine.config. That works - but I guess it's just a workaround I will have to address later. Thanks for input.

1
1
3/18/2015 3:04:07 PM

Accepted Answer

I've found a simple solution for that. You have to remove "name=" from the parameter string when you call entities constructor's base constructor. So you have to change this:

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

to this:

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

See it on here: Entity Framework MVVM Walk Through 1

5
3/12/2017 2:22:47 PM

Popular Answer

Change this:

base("name=NAV_HelpersEntities")

to this:

base(nameOrConnectionString: "NAV_HelpersEntities")


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow