169

I'm initializing Entity Framework Object context, and this gives me the keyword not supported error:

metadata=res://*/MainDB.csdl|res://*/MainDB.ssdl|res://*/MainDB.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Workspace\vs\Leftouch\Leftouch.Web\Data\Leftouch.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;App=EntityFramework"

I took the connection string directly from web.config which was working, and modified only the path to the file (which I set dynamically), and instead of using the default value, used this connection string explicitly. What could cause this error?

Can Poyrazoğlu
  • 31,161
  • 42
  • 171
  • 354
  • 2
    Please refer http://stackoverflow.com/questions/6003085/how-do-i-programmatically-set-the-connection-string-for-entity-framework-code-fi for a different approach – LCJ Jul 18 '12 at 12:31
  • 1
    I don't know who came up with this `metadata=res:`, then `res=somethingelse` with `"` all over the place syntax - but they should be really glad they aren't in the same room as me right now :-/ – Simon_Weaver Apr 20 '17 at 00:16
  • 2018 .Net EF Core similar syntax error - providerName was not needed by a SqlClient connection string. Also no quotes or ticks in string for EF core. – Sql Surfer Jun 26 '18 at 21:39

6 Answers6

338

The real reason you were getting this error is because of the " values in your connection string.

If you replace those with single quotes then it will work fine.

https://docs.microsoft.com/archive/blogs/rickandy/explicit-connection-string-for-ef

(Posted so others can get the fix faster than I did.)

Callum Watkins
  • 2,604
  • 3
  • 31
  • 47
Vaccano
  • 73,620
  • 138
  • 433
  • 790
38

I fixed this by changing EntityClient back to SqlClient, even though I was using Entity Framework.

So my complete connection string was in the format:

<add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Password=xxx" providerName="System.Data.SqlClient" />
Savage
  • 2,177
  • 1
  • 29
  • 34
14

This appears to be missing the providerName="System.Data.EntityClient" bit. Sure you got the whole thing?

Craig Stuntz
  • 124,853
  • 12
  • 249
  • 270
  • Ok, that was another attribute, forgot that one. Added it now it is `metadata=res://*/MainDB.csdl|res://*/MainDB.ssdl|res://*/MainDB.msl;provider=System.Data.SqlClient;provider name=System.Data.EntityClient;provider connection string="{0};App=EntityFramework"` and it now says key not found: provider name. I've also tried `providerName=...` instead of `provider name=` too, but no luck. – Can Poyrazoğlu Aug 09 '11 at 18:03
  • 2
    Well, I started to create it using the `EntityConnectionStringBuilder` class, and weirdly, it's working now. But I still have no idea why it wasn't accepting my string, even with your additions. – Can Poyrazoğlu Aug 09 '11 at 18:49
3

Believe it or not, renaming LinqPad.exe.config to LinqPad.config solved this problem.

Sameer Alibhai
  • 2,992
  • 4
  • 33
  • 36
2

Make sure you have Data Source and not DataSource in your connection string. The space is important. Trust me. I'm an idiot.

Hairgami_Master
  • 5,329
  • 9
  • 42
  • 64
1

Just use \" instead ", it should resolve the issue.