关键字不支持:'metadata'。在使用Entity Framework和MVC3中的Sql Connection时出现了此问题。

19

我正在使用Entity Framework 4与我的Asp.Net MVC3应用程序。我的问题是,我正在使用Entity Framework执行与我的数据库的操作,这很好。出于其他一些目的,我还使用Sql Connection存储和检索数据库中的数据。我遇到了

[Keyword not supported: 'metadata']

连接到我的数据库时发生错误。

这是我的Web配置文件。

  <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" />

我正在使用类库,这是我的应用配置。

   <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" />
4个回答

21

ADO.NET的连接字符串(在这种情况下是SqlConnection)不采用那种格式。您正在使用特定于Entity Framework的连接字符串。ADO.NET应该是类似于以下内容:

"data source=KAPS-PC\KAPSSERVER;initial catalog=vibrant;integrated security=True"

所以,总的来说,你需要两个不同的连接字符串,一个用于EF,一个用于ADO.NET。


19

你的连接字符串是特定于Entity Framework并包含元数据的。你需要从中获取提供程序连接字符串。您可以使用EntityConnectionStringBuilder来完成此操作:

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

1
短小精悍、简明易懂!谢谢。 - Devin Prejean
嗨,我应该在哪里编写这段代码?是在web.config文件中吗? - kurniawan26
@kurniawan26,你不能在web.config中编写C#代码。你需要在连接到数据库的任何地方使用它。但是你的连接字符串应该存储在web.config中。你可以在一个单独的问题中发布你的代码,我很乐意帮助你。 - Nikolai Samteladze

6

除了为同一事物使用两个单独的连接字符串之外,另一个选项是构建一个方法,从您的Entity Framework对象返回ADO.NET连接字符串:

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;
}

我把这个放在我的类库中,就在我的edmx文件所在的地方。

我从http://justgeeks.blogspot.com/2009/11/getting-sqlconnection-from.html获取了这个。

或者更好的方法是...如果你的SQLConnection是手动SQL查询,可以通过ExecuteStoredCommand完全跳过SQLConnection:

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

我从Entity Framework获取SQL连接中得到了这个。


-1

这里有一个更好的解决方案,使用实体框架工具,将你的数据库反向工程到你的项目中,然后你可以为两种情况使用单个EF连接字符串(这是一个普通的连接字符串)。


我没听懂你的意思,能否再解释一下? - Kaps Hasija

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接