0

I have been searching for a few hours now and the closest I have came to fixing it is have found this solution but I cannot seem to get it to work in my project.

I am trying to upload a .CSV file and add the contents of the .CSV file to my database. (I am using a Local Database). When I upload the file I get an error

System.ArgumentException: 'Keyword not supported: 'metadata'.

From the answer in the link I have provided the problem is because "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." but I am not sure how I can fix this.

My connection string is:

<add name="MyDatabaseEntities" connectionString="metadata=res://*/Models.MyModel.csdl|res://*/Models.MyModel.ssdl|res://*/Models.MyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\MyDatabase.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

This is the code in my controller where I am trying to connect to the database

    string conString = ConfigurationManager.ConnectionStrings
    ["MyDatabaseEntities"].ConnectionString;
    using (SqlConnection con = new SqlConnection(conString))
    {
    using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
    {
    //Set the database table name.
    sqlBulkCopy.DestinationTableName = "dbo.Orders"; 

Can someone please tell me how to fix this issue, any help is greatly appreciated.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
BrightLight
  • 5
  • 2
  • 4
  • What does it have to do with MySQL? – Oscar Feb 15 '18 at 14:56
  • more accurately it's an EF database first connection string for the edmx file. you can strip all the "metadata=...", there is a normal encoded connection string in there. – Fran Feb 15 '18 at 14:57
  • The linked answer tell's you what the error means. To fix it you need to provide a valid connection for your database. That is covered extensively on the web. – Stephen Kennedy Feb 15 '18 at 14:59
  • @StephenKennedy Do you recommend removing "metadata=res://*/Models.MyModel.csdl|res://*/Models.MyModel.ssdl|res://*/Models.MyModel.msl;provider=System.Data.SqlClient;provider" from the existing connection string? – BrightLight Feb 15 '18 at 16:06

1 Answers1

2

Just add a new connection string to your web.config using

<add name="SqlConnectionString" connectionString="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\MyDatabase.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" />

Use that new connection string.

string conString = ConfigurationManager.ConnectionStrings
    ["SqlConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(conString))
    {
    using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
    {
    //Set the database table name.
    sqlBulkCopy.DestinationTableName = "dbo.Orders"; 
Oscar
  • 13,594
  • 8
  • 47
  • 75
  • do I remove everything before "data source=" in the connection string? – BrightLight Feb 15 '18 at 15:22
  • @BrightLight All the stuff before "data source" is EF related metadata, an needs to be removed if you want to use the connection with a regular SqlConnection instead of EF. Just use the string in my answer as a new connection string and reference that when building your SqlConnection object – Oscar Feb 15 '18 at 16:08
  • ok I have changed my connection string to `` but that now has stopped the login functionality in the web app working. When I try to login I get an error `System.InvalidOperationException: 'The connection string 'MyDatabaseEntities' in the application's configuration file does not contain the required providerName attribute.`. Any idea how to fix this? – BrightLight Feb 15 '18 at 16:45
  • it is this line where I am getting the error `var v = dc.Users.Where(a => a.EmailID == login.EmailID).FirstOrDefault();` – BrightLight Feb 15 '18 at 16:46
  • @BrightLight No, don't change your existing connection, instead create a new one with a new name! Use this new connection string in your SqlConnection constructor. – Oscar Feb 15 '18 at 17:41
  • @BrightLight I have updated my answer, hope it's more clear now. – Oscar Feb 15 '18 at 17:43