Kap*_*ija 15 asp.net asp.net-mvc entity-framework asp.net-mvc-3 entity-framework-4.3
我正在使用我的Asp.Net MVC3应用程序实体框架4.我的问题是我正在使用Entity Framework对我的数据库执行操作,这很好.出于其他目的,我还使用Sql Connection来存储和检索数据库中的数据.我正进入(状态
[Keyword not supported: 'metadata']
Run Code Online (Sandbox Code Playgroud)
连接数据库时出错.
这是我的网络配置
<add name="VibrantEntities" connectionString="metadata=res://*/Vibrant.csdl|res://*/Vibrant.ssdl|res://*/Vibrant.msl;provider=System.Data.SqlClient;provider connection string="data source=KAPS-PC\KAPSSERVER;initial catalog=vibrant;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Run Code Online (Sandbox Code Playgroud)
我正在使用类库,所以这是我的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="data source=KAPS-PC\KAPSSERVER;initial catalog=vibrant;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Run Code Online (Sandbox Code Playgroud)
pso*_*usa 20
ADO.NET的连接字符串(在本例中SqlConnection)不采用该格式.您正在使用特定于Entity Framework的那个.ADO.NET应该是这样的:
"data source=KAPS-PC\KAPSSERVER;initial catalog=vibrant;integrated security=True"
Run Code Online (Sandbox Code Playgroud)
因此,总结一下,您需要两个单独的连接字符串,一个用于EF,另一个用于ADO.NET
Nik*_*dze 15
连接字符串特定于Entity Framework并包含元数据.您需要从中获取提供者连接字符串.你可以使用EntityConnectionStringBuilder:
var efConnectionString = "Your Entity Framework connection string";
var builder = new EntityConnectionStringBuilder(efConnectionString);
var regularConnectionString = builder.ProviderConnectionString;
Run Code Online (Sandbox Code Playgroud)
另一个选项(除了2个单独的连接字符串用于相同的事情)是构建一个从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;
}
Run Code Online (Sandbox Code Playgroud)
(我将它放在我的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));
Run Code Online (Sandbox Code Playgroud)
(我从Entity Framework得到这个获得sql连接)
| 归档时间: |
|
| 查看次数: |
25193 次 |
| 最近记录: |