Keyword not supported: 'metadata'.? with Sql Connection in Entityt Framework with MVC3

跟風遠走 提交于 2019-12-29 05:09:06

问题


I am using Entity Framework 4 with my Asp.Net MVC3 application. My problem is that I am using Entity Framework to perform action with my database , That is working fine. For some other purpose I am also using Sql Connection to Store and Retrieve my data from database. I am getting

[Keyword not supported: 'metadata']

error while connecting with my database.

This is my web config

  <add name="VibrantEntities" connectionString="metadata=res://*/Vibrant.csdl|res://*/Vibrant.ssdl|res://*/Vibrant.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=KAPS-PC\KAPSSERVER;initial catalog=vibrant;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

I am using class Library, So this is my App Config.

   <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

   <add name="VibrantEntities" connectionString="metadata=res://*/Vibrant.csdl|res://*/Vibrant.ssdl|res://*/Vibrant.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=KAPS-PC\KAPSSERVER;initial catalog=vibrant;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

回答1:


The connection string for ADO.NET (in this case SqlConnection) doesn't take that format. 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




回答2:


You connection string is specific to Entity Framework and contains metadata. You need to get your provider connection string from it. You can do it using EntityConnectionStringBuilder:

var efConnectionString = "Your Entity Framework connection string";
var builder = new EntityConnectionStringBuilder(efConnectionString);
var regularConnectionString = builder.ProviderConnectionString;



回答3:


Another option (other than 2 separate connection strings for the same thing) is to build a method that returns an ADO.NET connection string from your Entity Framework object:

using System.Data.EntityClient;
using System.Data.SqlClient;
...
private string GetADOConnectionString()
{
    SalesSyncEntities ctx = new SalesSyncEntities(); //create your entity object here
    EntityConnection ec = (EntityConnection)ctx.Connection;
    SqlConnection sc = (SqlConnection)ec.StoreConnection; //get the SQLConnection that your entity object would use
    string adoConnStr = sc.ConnectionString;
    return adoConnStr;
}

(I place this somewhere in my Class Library where my edmx files are)

(I got this from http://justgeeks.blogspot.com/2009/11/getting-sqlconnection-from.html)

Or even better... if your SQLConnection stuff are manual SQL queries, skip the SQLConnection entirely via the ExecuteStoredCommand:

new AdventureEntities().ExecuteStoreCommand(
        @"    UPDATE Users
              SET lname = @lname 
              WHERE Id = @id",
        new SqlParameter("lname", lname), new SqlParameter("id", id));

(I got this from Entity Framework getting an sql connection)




回答4:


Theres a better solution here, use the Entity Framework Power tools, reverse engineer your database into your project, then you can use a single EF connection string (which is a normal connection string) for both scenarios.



来源:https://stackoverflow.com/questions/12946099/keyword-not-supported-metadata-with-sql-connection-in-entityt-framework-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!