Keyword Not Supported: Metadata

C#Sql Serverasp.net MvcEntity Framework

C# Problem Overview


This line:

WebSecurity.InitializeDatabaseConnection(connectionStringName: "DefaultConnection", userTableName: "UserProfile", userIdColumn: "UserID", userNameColumn: "UserName", autoCreateTables: true);

Is throwing:

'System.ArgumentException' occurred in System.Data.dll but was not handled in user code

Additional information: Keyword not supported: 'metadata'.

My connection string is:

add name="DefaultConnection" connectionString="metadata=res://*/TalyllynModel.csdl|res://*/TalyllynModel.ssdl|res://*/TalyllynModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=***********;initial catalog=********;persist security info=True;user id=*********;password=********;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.SqlClient" /></connectionStrings>

Not sure where it is im going wrong.

C# Solutions


Solution 1 - C#

The string you passed is not a valid database connection string, it's an EF connection string that contains a SQL Server connection string in its provider connection string parameter. WebSecurity.InitializeDatabaseConnection expects a valid database connection string

To avoid parsing the connection string yourself, you can use the EntityConnectionStringBuilder class to parse the string and retrieve the database connection string from its ProviderConnectionString property

Solution 2 - C#

When this happened to me it was because the connection string had:

providerName="System.Data.SqlClient"

but it should be:

providerName="System.Data.EntityClient"

because as was said by the other answer, it is an EF connection string.

Solution 3 - C#

Just to add another possibility (which I encountered) - which might be the case if you're developing/maintaining an Azure WebApp, using a connection string saved in Azure's Application Settings.

Beside each connection string in the Application Settings is a dropdown for the connection string type - it's very easy to forget to set this to 'Custom' for Entity Framework values and leave it at the default (SQL Database) - which also causes the above error.

Solution 4 - C#

Here's some code I use, to extract the database name & server name from a connection string.

Notice how it checks if it's an Entity Framework connection string, and if so, it extracts the "provider connection string" part of that, which can then be passed into SqlConnectionStringBuilder:

If I didn't do this, I'd get that nasty "Keyword Not Supported: Metadata" error.

if (connectionString.ToLower().StartsWith("metadata="))
{
	System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder efBuilder = new System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder(connectionString);
	connectionString = efBuilder.ProviderConnectionString;
}

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
DatabaseServer = builder.DataSource;             //  eg "MikesServer"
DatabaseName = builder.InitialCatalog;           //  eg "Northwind"

Solution 5 - C#

I'm going to throw out another answer, just in case someone else runs into this through the same weird scenario as I did.

To start with, as others have said, ADO connection strings and EF connection strings are different.

An ADO connection string contains a number of semicolon-separated fields, which can very from one connection type to another, but you usually see "data source=xxx", "initial catalog=yyy", etc. You will not see "metadata=zzz".

An EF connection string has the same structure, but it has a "metadata=zzz" and a "provider connection string=www", where "www" is an escaped ADO connection string.

So a normal format for an ADO connection string is:

data source=myserver;
initial catalog=mydatabase;
Persist Security Info=True;
User ID=myusername;
Password=mypassword;
MultipleActiveResultSets=True

While a normal format for an EF connection string is:

metadata=res://*/MyDbContext.csdl|
    res://*/MyDbContext.ssdl|
    res://*/MyDbContext.msl;
provider=System.Data.SqlClient;
provider connection string=&quot;
    data source=myserver;
    initial catalog=mydatabase;
    Persist Security Info=True;
    User ID=myusername;
    Password=mypassword;
    MultipleActiveResultSets=True;
    application name=EntityFramework
    &quot;

Most folks who are running into this problem seem to have cut an EF connection string and pasted it into a place that needed an ADO connection string. In essence, I did the same thing, but the process wasn't as clear as all that.

In my case, I had a web application that used EF, so its web.config properly contained EF connection strings.

I published a deployment package, and the process prompts you for the connection strings to be used when deploying. These are stored in the deployment package's generated SetParameters.xml file.

I cut and pasted the EF connection strings into the publish dialog's entry fields.

I deployed the web application, tried to access it, and got the "Keyword not supported: metadata" error.

What I didn't realize is that MS's publish tool expected an ADO connection string, and that given it it would construct an EF connection string.

The result was that SetParameters.xml and my deployed web.config had connection strings that looked like this:

metadata=res://*/MyDbContext.csdl|
    res://*/MyDbContext.ssdl|
    res://*/MyDbContext.msl;
provider=System.Data.SqlClient;
provider connection string=&quot;
    metadata=res://*/XxDbContext.csdl|
        res://*/XxDbContext.ssdl|
        res://*/XxDbContext.msl;
    provider=System.Data.SqlClient;
    provider connection string=&amp;quot;
        data source=myserver;
        initial catalog=mydatabase;
        Persist Security Info=True;
        User ID=myusername;
        Password=mypassword;
        MultipleActiveResultSets=True;
        application name=EntityFramework
        &amp;quot;
    &quot;"

In other words, the embedded provider connection string was an EF connection string and not an ADO connection string, so when EF tried to use it to connect to the database, it generated this error.

In other words, when you are pasting the connection strings into the publish dialogues, you need to paste a ADO connection string, not an EF connection string, even if what you have in the web.config you are copying from is an EF connection string.

You can extract an ADO connection string from the provider connection string field of an EF connection string, and that's what you will need, if you're using the same connection in the deploy as you did in local development.

Solution 6 - C#

For use in Azure Application Settings => Connection Strings:

  1. If the connection string is generated by EF-designer be sure to replace &qout; with " in the string.

  2. Check that provider=System.Data.SqlClient

  3. Choose Type Custom in the dropdown

  4. If the connection is for a model (Entity Framework) ensure that correct path to your model is used Ex: A model  "MyWebRoot/Models/MyModel.edmx" is configured as: metadata=res:///Models.MyModel.csdl|res:///Models.MyModel.ssdl|res://*/Models.MyModel.msl;

Solution 7 - C#

> Hi, > > In my opinion, the connection string for ADO.NET (in this > caseSqlConnection) can't use 'metadata. You're using the one specific > for Entity Framework. The ADO.NET one should be something like: > > "data source=KAPS-PC\KAPSSERVER;initial catalog=vibrant;integrated security=True" > > So, to sum it up, you need two separate connection strings, one for EF > and one for ADO.NET. > > Souce: http://forums.iis.net/post/2097280.aspx

Solution 8 - C#

For Azure Web App, Connection string type has not "System.Data.EntityClient", Custom works good.

enter image description here

Solution 9 - C#

Dry This, Remove metadata Info from your ConnectionString.

Change this.

<add name="DefaultConnection" connectionString="metadata=res://*/TalyllynModel.csdl|res://*/TalyllynModel.ssdl|res://*/TalyllynModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=***********;initial catalog=********;persist security info=True;user id=*********;password=********;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.SqlClient" /></connectionStrings>

To

<add name="DefaultConnection" connectionString="data source=***********;initial catalog=********;persist security info=True;user id=*********;password=********;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.SqlClient" /></connectionStrings>

Solution 10 - C#

Before i give My Solution let me explain something , I got this problem too , im using EntityFramework and Ado.net you cant use Entity framework Connection string in ADo and vice versa , so what i did was in the Web.config file i left the EF Connection string(Metadata one) and in the Controller for ADO i Added the connection string which i got from the database(properties). add the ADO string like this : SqlConnection sql = new SqlConnection();

sql.ConnectionString = @"Data Source=.\alienbwr;Initial Catalog=ABTO_POS;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";(dont use my string)

Solution 11 - C#

An old post but my solution,

Unfortunately these didn't solve it for me using Azure Functions talking to a separate project (class library) with an EDMX.

I had to edit the Context.CS class constructor replacing the

: base ("Entities")

with

: base (ConfigurationManager.ConnectionStrings["Entities"].ConnectionString)

Hopefully this might help someone else in need.

Solution 12 - C#

Check in this place

<add name="ConnectionString" connectionString="Data Source=SMITH;Initial Catalog=db_ISMT;Persist Security Info=True;User ID=sa;Password=@darksoul45;MultipleActiveResultSets=True;Application Name=EntityFramework"
  providerName="System.Data.SqlClient" />

As you can see there's a two connection string one for ADO and another for the Login System or whatever you want. In my case, ConnectionString is for Login system so I've used that in:-

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
    SqlCommand cmd = null;
    SqlDataReader dr = null;
    protected void Page_Load(object sender, EventArgs e)
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionASPCoder1450View Question on Stackoverflow
Solution 1 - C#Panagiotis KanavosView Answer on Stackoverflow
Solution 2 - C#Will NewtonView Answer on Stackoverflow
Solution 3 - C#oflaheroView Answer on Stackoverflow
Solution 4 - C#Mike GledhillView Answer on Stackoverflow
Solution 5 - C#Jeff DegeView Answer on Stackoverflow
Solution 6 - C#Petter IvarssonView Answer on Stackoverflow
Solution 7 - C#Muhammad Rehan QadriView Answer on Stackoverflow
Solution 8 - C#SongView Answer on Stackoverflow
Solution 9 - C#Rafiqul IslamView Answer on Stackoverflow
Solution 10 - C#stannis BarracudaView Answer on Stackoverflow
Solution 11 - C#JacobView Answer on Stackoverflow
Solution 12 - C#Rabi ShresthaView Answer on Stackoverflow