0

I'm getting build time error when setting a static variable inside my database project layer, its being setting inside the Startup method inside Startup.cs of my ASP.NET Web API.

When this block of code is commented it compile and runs ok, just I don't get the connection string I need.

The worst part is that the error itself isn't described, there is nothing on error box that popup and it doesn't tell nothing more than there is an error.

The method is the following:

public Startup(IConfiguration configuration)
{
    Configuration = configuration;

    // init of bug block
    string cnn = configuration.GetConnectionString("MyConnectionStringConfig");
    MyProjectDataLayer.MyClassDataLayer.MyStaticMethodSettingConnectionString(strCnn);
    // end of bug block
}

The MyClassDataLayer and MyStaticMethodSettingConnectionString are public.

Now I realize that although the "Error List" is empty there is this error description on "Output" box:

C:\MyWebApi\Startup.cs(21,21,21,31): error CS0122: 'MyClassDataLayer' is inaccessible due to its protection level
Done building project "MyWebApi.csproj" -- FAILED.

The class is the following:

namespace MyProjectDataLayer
{
    public class MyClassDataLayer : DbContext
    {
        private static string _staticCnn = "";

        public static void MyStaticMethodSettingConnectionString(string cnnParam)
        {
            MyClassDataLayer._staticCnn = cnnParam;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySql(connectionString: MyClassDataLayer._staticCnn, mySqlOptions => mySqlOptions.CharSetBehavior(CharSetBehavior.NeverAppend));
        }

        public DbSet<Model001> md1 { get; set; }
        public DbSet<Model002> md2 { get; set; }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
joe
  • 1
  • 1
  • Whats the error ? You might want to use try catch block to catch the exception to understand the error – Chetan Jan 06 '22 at 01:36
  • How do you know there is an error if there is no "error description"? – Ňɏssa Pøngjǣrdenlarp Jan 06 '22 at 01:39
  • @Chetan , I found a description on "Output" box, its " 'MyClassDataLayer' is inaccessible due to its protection level" – joe Jan 06 '22 at 01:50
  • @Ňɏssa Pøngjǣrdenlarp , because i received a box with "There were build errors. would you like to continue and run the last successful build? [YES] [NO]" but "Error List" was empty. – joe Jan 06 '22 at 01:51
  • @Chetan, I put the try catch block now but it will not throw because it don't even run, its a compile time error, it pop the message: "There were build errors. would you like to continue and run the last successful build? [YES] [NO]" – joe Jan 06 '22 at 01:56
  • Realized it is complaining about protection level but all its public – joe Jan 06 '22 at 01:58
  • Can you share the relevant code of `MyClassDataLayer` and `MyStaticMethodSettingConnectionString` ? – Chetan Jan 06 '22 at 02:03
  • @Chetan, its on question body now. – joe Jan 06 '22 at 02:10

1 Answers1

0

The problem is the class constructor, when you not specify the protection level of your constructor it is assumed to be internal by default as you can see here.

It can be a problem too if your properties lack a getter as you can see here.

Just add a public constructor on your "MyClassDataLayer" class and it will works fine.

Joe
  • 1