C# 无法解析为有效的类型或函数
我正在尝试为sql数据库中的表值函数构建OData端点。我的路由和控制器是正确的,但是我得到了错误C# 无法解析为有效的类型或函数,c#,sql,rest,asp.net-web-api,odata,C#,Sql,Rest,Asp.net Web Api,Odata,我正在尝试为sql数据库中的表值函数构建OData端点。我的路由和控制器是正确的,但是我得到了错误 'DataAccessFunctionEntities.GetConfiguration' ---> DataAccess.. is where I built a model cannot be resolved into a valid type or function. 尽管Get Configuration功能在我的控制器中 我尝试过调试,但得到了相同的错误。我找不到解决此错误的
'DataAccessFunctionEntities.GetConfiguration' ---> DataAccess.. is where I built a model
cannot be resolved into a valid type or function.
尽管Get Configuration功能在我的控制器中
我尝试过调试,但得到了相同的错误。我找不到解决此错误的方法
谢谢
调用DB函数(控制器内部)时出现错误:DB函数:
[DbFunction("DataAccessFunctionEntities", "GetConfiguration")]
public virtual IQueryable<GetConfiguration_Result> GetConfiguration(string partialPIDs)
{
var partialPIDsParameter = partialPIDs != null ?
new ObjectParameter("PartialPIDs", partialPIDs) :
new ObjectParameter("PartialPIDs", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<GetConfiguration_Result>("[DataAccessFunctionEntities].[GetConfiguration](@PartialPIDs)", partialPIDsParameter);
}
[DbFunction(“DataAccessFunctionEntities”,“GetConfiguration”)]
公共虚拟IQueryable GetConfiguration(字符串partialPIDs)
{
var partialPIDs参数=partialPIDs!=null?
新的ObjectParameter(“PartialPIDs”,PartialPIDs):
新的ObjectParameter(“PartialPIDs”,typeof(string));
返回((IObjectContextAdapter)this.ObjectContext.CreateQuery(“[DataAccessFunctionEntities].[GetConfiguration](@PartialPIDs)”,PartialPIDs参数);
}
路由将请求定向到正确的函数。以下是控制器的功能:
[HttpGet,EnableQuery]
public IHttpActionResult GetConfiguration(ODataQueryOptions<GetConfiguration_Result> options)
{
string errorType = string.Empty;
string exServerName = string.Empty;
int exErrorNumber = 0;
string exErrorMessage = string.Empty;
string exStackTrace = string.Empty;
var inputparameter = "NONE";
if (options == null)
throw new ArgumentNullException("options");
var uri = options.Request.RequestUri.ToString();
//Decode url for Page-2 and beyond
if (uri.Contains("%"))
{
uri = Uri.UnescapeDataString(uri);
}
var firstOpenParenthesisIndex = uri.Substring(uri.IndexOf("(", StringComparison.OrdinalIgnoreCase) + 1);
var equalsOperatorIndex = firstOpenParenthesisIndex.Substring(firstOpenParenthesisIndex.IndexOf("=", StringComparison.OrdinalIgnoreCase) + 1);
var partialPIds = equalsOperatorIndex.Substring(0, equalsOperatorIndex.IndexOf(")", StringComparison.OrdinalIgnoreCase));
inputparameter = partialPIds;
var validationResponse = ProcessInput.ValidatePartialPID(partialPIds);
// Intial Request Logging
var requestLog = new RequestLog
{
Source = "Configuration-GET",
RequestUrl = options.Request.RequestUri.ToString(),
UserName = User.Identity.Name,
StartTime = DateTime.Now,
EndTime = null,
RequestStatus = RequestStatus.STARTED.ToString(),
RequestLength = validationResponse.ParameterCount,
ResponseLength = 0,
QueryOptions = ProcessInput.ExtractQueryOptions(options.Request.RequestUri),
ParentId = null
};
var errorlog = new ErrorLog
{
RequestLogId = 0,
ErrorCode = 0,
ErrorDescription = ""
};
if (isLoggingEnabled)
{
requestLog.ParentId = _dbLogHelper.LogRequest(requestLog);
}
try
{
if (validationResponse.IsValidInput == false)
{
requestLog.RequestStatus = RequestStatus.FAIL.ToString();
_shortMessage = string.Format(_validationErrorShortMessage, requestLog.Source, validationResponse.InvalidInputMessage, CultureInfo.InvariantCulture);
_type = EventLogEntryType.Error;
_returnCode = ReturnCodes.ValidationErrorCode;
_returnCodeCategory = ReturnCodes.ValidationErrorCode;
errorType = "ValidationError";
throw new PAIntelODataException(
_shortMessage,
ODataLogCategory.ValidationException,
ReturnCodes.ValidationErrorCode,
requestLog.Source);
}
partialPIds = validationResponse.XmlPartialPIDs.ToString();
IQueryable<GetConfiguration_Result> result;
try
{
result = _DataAccessFunction.GetConfiguration(partialPIds);
}
catch (Exception ex)
{
requestLog.RequestStatus = RequestStatus.FAIL.ToString();
_type = EventLogEntryType.Error;
_returnCode = ReturnCodes.DatabaseErrorCode;
_returnCodeCategory = ReturnCodes.DatabaseErrorCode;
var innerException = ex;
if (ex.InnerException != null)
{
while (!(innerException is SqlException))
{
innerException = innerException.InnerException;
}
var sqlEx = innerException as SqlException;
_shortMessage = string.Format(_sqlErrorShortMessage, requestLog.Source, CultureInfo.InvariantCulture);
errorType = "SQLError";
exServerName = sqlEx.Server;
exErrorNumber = sqlEx.Number;
exErrorMessage = sqlEx.Message;
exStackTrace = sqlEx.StackTrace;
throw new PAIntelODataException(
_shortMessage,
sqlEx,
ODataLogCategory.DbException,
ReturnCodes.DatabaseErrorCode,
sqlEx.Source);
}
_shortMessage = string.Format(_sqlErrorShortMessage, requestLog.Source, CultureInfo.InvariantCulture);
errorType = "SQLGenearalError";
exErrorMessage = ex.Message;
exStackTrace = ex.StackTrace;
throw new PAIntelODataException(
_shortMessage,
ex,
ODataLogCategory.DbException,
ReturnCodes.DatabaseErrorCode,
requestLog.Source);
}
if (result != null)
{
requestLog.RequestStatus = RequestStatus.SUCCESS.ToString();
requestLog.ResponseLength = result.ToString().Length;
_type = EventLogEntryType.Information;
_returnCode = ReturnCodes.Success;
_returnCodeCategory = ReturnCodes.Success;
errorType = "None";
_message = string.Format(_successfulCall, requestLog.Source, CultureInfo.InvariantCulture);
return Ok(result, result.GetType());
}
return null;
}
catch (PAIntelODataException)
{
throw;
}
finally
{
requestLog.EndTime = DateTime.Now;
//Perform Logging
//Database Logging:
if (isLoggingEnabled)
{
var ChildId = _dbLogHelper.LogRequest(requestLog);
if (_type == EventLogEntryType.Error)
{
errorlog.RequestLogId = ChildId;
errorlog.ErrorCode = _returnCode;
errorlog.ErrorDescription = _shortMessage;
_dbLogHelper.LogErrorDetails(errorlog);
}
}
//EventViewer Logging:
if (_type == EventLogEntryType.Error)
{
switch (errorType)
{
case "ValidationError":
_message = string.Format(_validationErrorLongMessage, requestLog.Source, requestLog.Source, requestLog.RequestUrl, inputparameter, requestLog.QueryOptions, errorlog.RequestLogId, requestLog.UserName, requestLog.StartTime, requestLog.EndTime, validationResponse.InvalidInputMessage, CultureInfo.InvariantCulture); ;
break;
case "SQLError":
_message = string.Format(_sqlErrorLongMessage, requestLog.Source, exServerName, exErrorNumber, requestLog.Source, requestLog.RequestUrl, inputparameter, requestLog.QueryOptions, errorlog.RequestLogId, requestLog.UserName, requestLog.StartTime, requestLog.EndTime, exErrorMessage, exStackTrace, CultureInfo.InvariantCulture);
break;
case "SQLGenearalError":
_message = string.Format(_sqlErrorLongAbbreviatedMessage, requestLog.Source, requestLog.Source, requestLog.RequestUrl, inputparameter, requestLog.QueryOptions, errorlog.RequestLogId, requestLog.UserName, requestLog.StartTime, requestLog.EndTime, exErrorMessage, exStackTrace, CultureInfo.InvariantCulture);
break;
}
LogHelper.WriteToEventLog(_message, requestLog.Source, _type, _returnCode, _returnCodeCategory);
}
}
}
protected override void Dispose(bool disposing)
{
if (disposing && _AccessModel != null)
{
_AccessModel.Dispose();
}
if (disposing && _DataAccessFunction != null)
{
_DataAccessFunction.Dispose();
}
base.Dispose(disposing);
}
}
}
[HttpGet,EnableQuery]
公共IHttpActionResult GetConfiguration(ODataQueryOptions选项)
{
string errorType=string.Empty;
string exServerName=string.Empty;
int exercrorNumber=0;
string exercormessage=string.Empty;
string exStackTrace=string.Empty;
var inputparameter=“无”;
如果(选项==null)
抛出新的ArgumentNullException(“选项”);
var uri=options.Request.RequestUri.ToString();
//解码第2页及以上的url
if(uri.Contains(“%”)
{
uri=uri.UnescapeDataString(uri);
}
var firstOpen括号索引=uri.Substring(uri.IndexOf(“(”,StringComparison.OrdinalIgnoreCase)+1);
var equalsOperatorIndex=firstOpen括号索引.Substring(firstOpen括号索引.IndexOf(“=”,StringComparison.OrdinalIgnoreCase)+1);
var partialPIds=equalsOperatorIndex.Substring(0,equalsOperatorIndex.IndexOf(“)”,StringComparison.OrdinalIgnoreCase));
inputparameter=partialPIds;
var validationResponse=ProcessInput.ValidatePartialPID(PartialPID);
//初始请求日志记录
var requestLog=新的requestLog
{
Source=“配置获取”,
RequestUrl=options.Request.RequestUri.ToString(),
用户名=User.Identity.Name,
StartTime=日期时间。现在,
EndTime=null,
RequestStatus=RequestStatus.STARTED.ToString(),
RequestLength=validationResponse.ParameterCount,
响应长度=0,
QueryOptions=ProcessInput.ExtractQueryOptions(options.Request.RequestUri),
ParentId=null
};
var errorlog=新的errorlog
{
RequestLogId=0,
ErrorCode=0,
ErrorDescription=“”
};
如果(isLoggingEnabled)
{
requestLog.ParentId=_dbLogHelper.LogRequest(requestLog);
}
尝试
{
if(validationResponse.IsValidInput==false)
{
requestLog.RequestStatus=RequestStatus.FAIL.ToString();
_shortMessage=string.Format(_validationErrorShortMessage,requestLog.Source,validationResponse.InvalidInputMessage,CultureInfo.InvariantCulture);
_type=EventLogEntryType.Error;
_returnCode=ReturnCodes.ValidationErrorCode;
_returnCodeCategory=ReturnCodes.ValidationErrorCode;
errorType=“ValidationError”;
抛出新的数据异常(
_短消息,
ODataLogCategory.ValidationException,
ReturnCodes.ValidationErrorCode,
requestLog.Source);
}
partialPIds=validationResponse.XmlPartialPIDs.ToString();
可预测的结果;
尝试
{
结果=_DataAccessFunction.GetConfiguration(partialPIds);
}
捕获(例外情况除外)
{
requestLog.RequestStatus=RequestStatus.FAIL.ToString();
_type=EventLogEntryType.Error;
_returnCode=ReturnCodes.DatabaseErrorCode;
_returnCodeCategory=ReturnCodes.DatabaseErrorCode;
var innerException=ex;
if(例如InnerException!=null)
{
而(!(innerException是SqlException))
{
innerException=innerException.innerException;
}
var sqlEx=innerException作为SqlException;
_shortMessage=string.Format(_sqlErrorShortMessage,requestLog.Source,CultureInfo.InvariantCulture);
errorType=“SQLError”;
exServerName=sqlEx.Server;
exercrorNumber=sqlEx.Number;
exercorMessage=sqlEx.Message;
exStackTrace=sqlEx.StackTrace;
抛出新的数据异常(
_短消息,
sqlEx,
ODataLogCategory.DbException,
ReturnCodes.DatabaseErrorCode,
sqlEx.Source);
}
_shortMessage=string.Format(_sqlErrorShortMessage,requestLog.Source,CultureInfo.InvariantCulture);
errorType=“sqlgeneralerror”;
exercrorMessage=例如消息;
modelBuilder.Conventions.Add(new FunctionsConvention<MyDataContext>("schemaName"));
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Add functions on to entity model.
modelBuilder.Conventions.Add(new FunctionConvention<xxxxxYourEntitiesxxxxx>());
// tvf types
modelBuilder.ComplexType<GetConfiguration_Result>();
}
[ComplexType]
public class GetConfiguration_Result
{
//..
}
[Function(FunctionType.TableValuedFunction, "GetConfiguration", namespaceName : "xxxxxYourEntitiesxxxxx", Schema = "dbo")]
public virtual IQueryable<GetConfiguration_Result> GetConfiguration(string partialPIDs)