我在edmx中生成了用户定义的函数:
<Schema Namespace="DealerMaintenance.Store" Provider="Oracle.ManagedDataAccess.Client" ProviderManifestToken="11.2" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
<Function Name="GET_NEXT_DEALER_KY" Aggregate="false" BuiltIn="false"
NiladicFunction="false" IsComposable="true"
ParameterTypeSemantics="AllowImplicitConversion" Schema="MY_SCHEMA"
ReturnType="number" />
我试过了:
public partial class DealerContext : DbContext
{
[DbFunction("DealerMaintenance.Store", "GET_NEXT_DEALER_KY")]
public int? GetNextDealerKy()
{
var lObjectContext = ((IObjectContextAdapter)this).ObjectContext;
var output = lObjectContext.
CreateQuery<int?>("DealerMaintenance.Store.GET_NEXT_DEALER_KY")
.Execute(MergeOption.NoTracking)
.FirstOrDefault();
return output;
}
}
使用:
调用using (var objectContext = new DealerContext())
{
var x = objectContext.GetNextDealerKy();
}
得到了:
“System.Data.Entity.Core.EntitySqlException”类型的异常 发生在EntityFramework.dll中但未在用户代码中处理
其他信息:'DealerMaintenance.Store.GET_NEXT_DEALER_KY' 无法在当前范围或上下文中解决。确保这一点 所有引用的变量都在范围内,需要的模式是 已加载,并且正确引用了名称空间。近会员 访问表达式,第1行,第24列。
我也试过以下的变体:
using (var objectContext = new DealerContext())
{
var queryText = "SELECT DealerMaintenance.Store.GET_NEXT_DEALER_KY() FROM dual";
ObjectQuery<int> adQuery = ((IObjectContextAdapter)objectContext).ObjectContext.CreateQuery<int>(queryText);
var x = adQuery.First();
}
得到了:
'dual'无法在当前范围或上下文中解析。确保所有引用的变量都在范围内,加载了所需的模式,并且正确引用了这些名称空间。
答案 0 :(得分:0)
我无法直接使用该功能。解决方法是使用oracle命令:
using (DealerContext objectContext = new DealerContext())
{
string connString = objectContext.Database.Connection.ConnectionString;
using (var conn = new OracleConnection(connString))
{
using (var cmd = new OracleCommand())
{
cmd.Connection = conn;
cmd.CommandText = "select MY_SCHEMA.GET_NEXT_DEALER_KY FROM DUAL";
cmd.CommandType = CommandType.Text;
cmd.Connection.Open();
try
{
var kyo = (decimal)cmd.ExecuteScalar();
return Decimal.ToInt32(kyo);
}
finally
{
cmd.Connection.Close();
}
}
}
}
使用上下文类的:
public int GetNextDealerKy()
{
string connString = this.Database.Connection.ConnectionString;
using (var conn = new OracleConnection(connString))
{
using (var cmd = new OracleCommand())
{
cmd.Connection = conn;
cmd.CommandText = "select MY_SCHEMA.GET_NEXT_DEALER_KY FROM DUAL";
cmd.CommandType = CommandType.Text;
cmd.Connection.Open();
try
{
var kyo = (decimal)cmd.ExecuteScalar();
return Decimal.ToInt32(kyo);
}
finally
{
cmd.Connection.Close();
}
}
}
}