Getting Error The argument 'nameOrConnectionString' cannot be null, empty or contain only white space with Azure Database

12,165

Solution 1

If you have multiple projects under solution, try setting up the startup project will resolve this issue.(sry for the delay)

Solution 2

A spelling error in appsettings.json caused the same System.ArgumentException for me. I discovered the string "ConnectionStrings" had turned into "CondfSnectionStrings" I dunno why. My eye just glossed over the problem until another programmer pointed it out to me.

Share:
12,165

Related videos on Youtube

CodeScanner
Author by

CodeScanner

Updated on September 16, 2022

Comments

  • CodeScanner
    CodeScanner over 1 year

    I am having my database and other storage on azure cloud storage.i am using Entity Code first appoach but problem is when i am trying to read connection string from cloud storage using CloudConfigurationManager.GetSetting() i am getting below error:

    Error:The argument 'nameOrConnectionString' cannot be null, empty or contain only white space. An exception of type 'System.ArgumentException' occurred in EntityFramework.dll but was not handled in user code

    This is my Azure project:Demo.Web.Azure

    It has 2 configurations file:

    1)ServiceConfiguration.Cloud.cscfg:

    <Role name="Demo.Web">
     <Setting name="MyConnectionString"
     value="Server=----;Database=DemoEntity;User ID=---;Password=---&amp;w;Trusted_Connection=False;
    Encrypt=True;MultipleActiveResultSets=True;" />
    
      <Setting name="Demo.Storage"
     value="DefaultEndpointsProtocol=https;AccountName=---;AccountKey=------"
     />
    

    2)ServiceConfiguration.Local.cscfg:

    <Role name="Demo.Worker">
      <Setting name="Demo.Storage"
     value="DefaultEndpointsProtocol=https;AccountName=---;AccountKey=------"
     />
    

    3)ServiceDefinition.csdef:

    <ServiceDefinition name="Demo.Web.Azure" schemaVersion="2014-06.2.4">
          <WebRole name="RepuGuard.Web" vmsize="Small">
             <ConfigurationSettings>
                      <Setting name="MyConnectionString" />
    
                      <Setting name="Demo.Storage" />
                     </ConfigurationSettings>
    
         </WebRole>
    
     <WorkerRole name="Demo.Worker" vmsize="Small">
                    <ConfigurationSettings>
                      <Setting name="MyConnectionString" />
    
                      <Setting name="Demo.Storage" />
                     </ConfigurationSettings>
    
     </WorkerRole>
    
    </ServiceDefinition>
    

    This is my Context file which read Database connection string:

    public partial class MyDemoDBContext : DbContext, IDisposable
        {
               public ObjectContext Context { get; set; }
               public MyDemoDBContext()
                : base(CloudConfigurationManager.GetSetting("MyConnectionString"))//Getting Error here
               {
                 Context = ((IObjectContextAdapter)this).ObjectContext;
               }
                 public MyDemoDBContext (string connectionString)
    
                : base(connectionString)
    
                 {
    
                     Context = ((IObjectContextAdapter)this).ObjectContext;
    
                 }
    }
    

    Getting error here:

    public MyDemoDBContext()
                : base(CloudConfigurationManager.GetSetting("MyConnectionString"))
               {
                 Context = ((IObjectContextAdapter)this).ObjectContext;
               }
    
  • malarres
    malarres almost 9 years
    welcome to stackoverflow :) you could consider to elaborate your answer. e.g. post some code about how to set up the start up project.
  • TheNoob
    TheNoob over 8 years
    @malarres Should he provide that? Right click in solution explorer.... AT SteveLillis Maybe this solved his problem before, which is why its here? I doubt he can comment on the OP anyways. anyways, if op still needs help I found this nopcommerce.com/boards/t/34504/… which ended up helping me solve the issue
  • malarres
    malarres over 8 years
    @TheNoob that's why an e.g. is an e.g. and not an i.e. ;-P your idea is perfect as a solution: direct the rest of the readers through a contextual menu. for extra goodness remember a) the key tag and b) that you can upload screenshots. And please make your own answer based on that link, as you know that page can dissapear and leave a broken link here. thanks
  • Brandon Gano
    Brandon Gano almost 8 years
    This absolutely solved the problem for me. Visual Studio (2015 Community) had incorrectly set my data project as the startup project. Manually changing the web project to default fixed the issue. (Right-click the web project in VS and choose Set as StartUp Project)