0

I have a static constructor that never gets called - in fact CheckedIn is always set to true somehow the code below is from my class library

    public partial class Commerce : DbContext
    {
...
        static Commerce()
        {
            CheckedIn = false;
            IsUpToDate = false;
            CheckSchema();
        }

        public static bool CheckSchema(Action<string,Exception> logMethod = null, bool force = false)
        {

            if (CheckedIn)
                return IsUpToDate;

            CheckedIn = true;
...
    }

the code below is a vb.net code behind page invoking the CheckSchema() method on the class above. When it calls, the constructor is never called:

        If (Commerce.Database.Model.Commerce.CheckSchema()) Then
            Return True
        End If
Sarfaraz Jamal
  • 343
  • 4
  • 15
  • The static constructor is called once for a class. So has there been usages for "Commerce" before? Presumably yes it must have been instantiated. – Ralf Jun 08 '22 at 15:22
  • Put a breakpoint in your static constructor and fire up the app in a debugger. Or, put in a `Console.WriteLine` (or `Debug.WriteLine`) call in it. You will set it get called – Flydog57 Jun 08 '22 at 15:24
  • [When is a static constructor called?](https://stackoverflow.com/questions/1437352/when-is-a-static-constructor-called-in-c) – Jonesopolis Jun 08 '22 at 15:26
  • If `CheckedIn` is set to true, then by definition the `CheckSchema` must have been executed (unless there is other code that sets that value to `true` that you haven't shown us). Also, this looks like a code smell to me anyway. Having anything static on a DbContext is inherently dangerous. – DavidG Jun 08 '22 at 15:29
  • I have put a breakpoint on the constructor - it never gets in. – Sarfaraz Jamal Jun 08 '22 at 15:33
  • It is possible that the break point is never hit, if the line was executed before debugger is attached. – Aamir Masood Jun 08 '22 at 15:40
  • If that's true, we'll need a [mcve] – Steven Doggart Jun 08 '22 at 15:40
  • Thanks! How can i close or delete a question? upon further debugging I realized I was calling the method in application_start in global.asax and since I was debugging it in IIS (not IIS Express) - the breakpoints were never hit – Sarfaraz Jamal Jun 08 '22 at 17:41
  • Write your findings as an answer and accept it. Others might run into the same issue and would welcome a hint as to where to look. – Hel O'Ween Jun 09 '22 at 08:57

0 Answers0