我正在尝试使用 Microsoft 实体框架连接我的 PostgreSQL 数据库。
由于 npgsql 数据库提供程序没有集成到 Visual Studio 中,我使用 EdmGen 工具从我现有的数据库中生成接口代码。
该数据库还包括一个用于测试目的的存储过程:它不接受参数并返回 void。EdmGen 不会为此存储过程生成任何代码 - 但它包含在生成的 ssdl 文件中。所以我写了一个小工具来修改 csdl 和 msl 文件以包含这个过程。这是三个文件的简化视图:
ssdl:
<Schema Namespace="Test.Store" Alias="Self" Provider="Npgsql" ProviderManifestToken="9.2.4" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
<Function Name="TestFunc" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="public" />
</Schema>
csdl:
<Schema Namespace="Test" Alias="Self" p1:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns:p1="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<EntityContainer Name="TestContext" p1:LazyLoadingEnabled="true">
<FunctionImport Name="TestFunc" IsComposable="false" />
</EntityContainer>
</Schema>
msl:
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
<EntityContainerMapping StorageEntityContainer="TestStoreContainer" CdmEntityContainer="TestContext">
<FunctionImportMapping FunctionImportName="TestFunc" FunctionName="Test.Store.TestFunc" />
</EntityContainerMapping>
</Mapping>
EdmGen 生成的代码如下所示:
public int TestFunc()
{
return base.ExecuteFunction("TestContext.TestFunc");
}
我使用以下代码调用此函数:
using (var ctx = new TestContext())
{
ctx.TestFunc();
}
但我得到以下异常:
[System.Data.EntityCommandCompilationException]
准备命令定义时出错。有关详细信息,请参阅内部异常。
内部异常是:
[System.ArgumentException]
值不在预期范围内。
我不知道这个异常是从哪里来的,因为没有值可以传入或传出存储过程。也许你们可以帮助我。
可能也很重要的几点:
这是我的连接字符串
app.config
:<connectionStrings> <add name="TestContext" providerName="System.Data.EntityClient" connectionString="metadata=res://Test/Test.Database.Test.csdl|res://Test/Test.Database.Test.ssdl|res://Test/Test.Database.Test.msl; provider=Npgsql; provider connection string="Server=localhost;Port=5432;Database=Test;User Id=xxx;Password=xxx;enlist=true;""/> </connectionStrings>
我正在使用以下软件版本:
- Visual Studio Express for Desktop 2012
- npgsql 2.0.11.0
我对 Entity Framework 和 ADO.NET 完全陌生,对 C#、.NET 和 Visual Studio 也很陌生。所以我可能误解了一些概念。