172

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?

3
  • 2
    Please refer stackoverflow.com/questions/6003085/… for a different approach
    – LCJ
    Commented Jul 18, 2012 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 :-/ Commented Apr 20, 2017 at 0: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
    Commented Jun 26, 2018 at 21:39

6 Answers 6

340

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://learn.microsoft.com/archive/blogs/rickandy/explicit-connection-string-for-ef

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

4
  • 1
    If you are passing in the connection string to the ObjectContent class, make sure that it has single quotes. If you are getting the connection from the .config file, then it is okay to use the " escape sequence. Commented May 10, 2012 at 23:45
  • For those of you not familiar with how XML works, " is an escape sequence for a quotation mark because it is a reserved character in XML. Commented Oct 9, 2012 at 14:38
  • System.Data.EntityClient.EntityConnectionStringBuilder made it for me, thank you.
    – AFract
    Commented Sep 15, 2015 at 14:02
  • The blog link in your post gave me awareness about EntityConnectionStringBuilder class. Thank you. +1.
    – RBT
    Commented Dec 25, 2016 at 2:09
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" />
2
  • 6
    The above change will potentially lead to (as in my case) the following error "The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development"
    – Psi-Ed
    Commented Dec 19, 2016 at 9:45
  • You can comment throw new UnintentionalCodeFirstException() in OnModelCreating(), but it looks like a dirty workaround Commented Sep 5, 2018 at 9:36
14

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

2
  • 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=&quot;{0};App=EntityFramework&quot; and it now says key not found: provider name. I've also tried providerName=... instead of provider name= too, but no luck. Commented Aug 9, 2011 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. Commented Aug 9, 2011 at 18:49
3

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

4
  • 13
    This is Microsoft, anything can be happened ! Commented Jul 6, 2015 at 15:17
  • This doesn't make sense. What has Linqpad to do with the question and how can renaming an executable ever be helpful? Commented Dec 26, 2019 at 21:15
  • Well this particular error occured with Linqpad in my case and doing this resolved it. And I didn't rename the executable. I renamed the .exe.config to .config. Commented Dec 27, 2019 at 1:07
  • Anyway, it's a very specific scenario. It only adds noise as it doesn't explain anything, Commented Dec 27, 2019 at 20:56
2

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

0
1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.