0

This is a strange case. I am setting the connection in my db context constructor.

 Database.Connection.ConnectionString = "server=myserver,55555;Initial Catalog=mycatalog;User id=myuser;Password=myuserspassword;"

This is failing with the Format of the initialization string does not conform to specification starting at index 0.

What is strange is that if I change the code to run the same command a second time, it succeeds on the second attempt.

var constr =  "server=myserver,55555;Initial Catalog=mycatalog;User id=myuser;Password=myuserspassword;";
 try
 {
     Database.Connection.ConnectionString = constr;
 }
 catch
 {
      Database.Connection.ConnectionString = constr;
 }

So it seems the string is ok right? I verified that by screwing up the string and then it consistently throws the format error on all attempts. So it seems that something is causing it to throw the error only the first time I attempt to set the string.

UPDATE: When I hover over Database.Connection before even setting the connection string the error is already sitting there and I can't inspect the Connection property. If I mouse away and mouse back over it then the error is gone and it takes my string on the first attempt. So it seems like that error is being caused before I even get the constructor for the db context. Doesn't really answer the question, but its a clue.

What could that be?

Josh
  • 1,648
  • 8
  • 27
  • 58
  • Have a look at this https://stackoverflow.com/questions/8243008/format-of-the-initialization-string-does-not-conform-to-specification-starting-a – Salah Akbari Dec 29 '17 at 19:06
  • @S.Akbari that question does not address my issue, which is that this fails on the first attempt and succeeds with the same connection string on the second attempt. Therefore the string is valid, but something else is causing it to fail. – Josh Dec 29 '17 at 19:09
  • And what is that 55555 in connection string? – Evk Dec 29 '17 at 19:22
  • @Evk That is a port number – Josh Dec 29 '17 at 19:34
  • @Josh I thought port is usually defines with `:` like this `server=myserver:55555;` – Salah Akbari Dec 29 '17 at 19:36
  • Indeed, I didn't know such strange format is used for specifying port (since I never connected to sql server on custom port). – Evk Dec 29 '17 at 19:37
  • Well, anyway I cannot reproduce this, your connection string works fine for me with a first try. – Evk Dec 29 '17 at 19:45
  • @Evk see my update, pretty big clue – Josh Dec 29 '17 at 20:45
  • @josh When you hover over `Database.Connection`, keep in mind that the debugger visualizer will run some code (it has to, to evaluate an expression). It *can* have side effects. Yeah, the simple act of using the debugger can sometimes trigger unknown side effects. An easy example of this: make a class, override `ToString()`, make an instance of the class, set a breakpoint inside `ToString()`, then look at the class instance in the debugger. You'll hit your breakpoint. –  Dec 29 '17 at 20:49
  • I dont think this is answerable without a [mcve]. –  Dec 29 '17 at 20:56
  • @Amy thanks! I actually figured it out, we issue with EF finding the bad string in my config before I had time to decrypt it. I didn't think EF would setup a connection automatically before even hitting the constructor, but apparently it does. – Josh Dec 29 '17 at 21:10
  • @josh it's the parent constructor that does that. your constructor is called, but before it runs, the base is called. `DbContext` automagically sets up a connection, somewhere deep inside the call to `InitializeLazyInternalContext`. the EF source code is pretty inscrutable, so tracking this down will be difficult, but you can find it if you search long and hard enough. –  Dec 29 '17 at 21:15

1 Answers1

0

Figured out the issue. My connection string is stored in an encrypted format, and I moved them from appSettings to connectionStrings area of my webconfig. Because of that move, EF was finding the encrypted string and setting up a connection before it even gets to the constructor of the db context. Then it throws the error the first time you try to access the connection property.

Solution was to just move the connection strings back to appsettings to EF can't find them automaticaly.

Josh
  • 1,648
  • 8
  • 27
  • 58