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

c# entity-framework-6 wpf

Question

I am aware that this has been asked before and has generally received the same response. However, in these situations, the issue frequently arises when multiple projects use the same entity and the connectionstring is absent from all config files. All of my work is on the same project. Additionally, App.config contains the connection string. In XAML, I'm utilizing a 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>...

I'm using my view model to call my entity:

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

It functions if I call the entity from other locations in my viewmodel. binding from code behind is an illustration.

The connectionstring (which, when used in MainWindow.cs, is functional):

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

The constructor for NAV HelpersEntities is as follows:

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

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

Why am I doing this wrong?

UPDATE: Okay, adding the connectionstring to machine.config allowed me to get it working for the time being. That works, but I suppose it's simply a temporary fix that I will have to deal with in the future. Thanks for the advice.

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

Accepted Answer

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

Popular Answer

Modify this:

base("name=NAV_HelpersEntities")

on 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