我正在尝试为sql数据库中的Table值函数构建一个OData端点。我的路由和控制器是正确的,但我收到错误
'DataAccessFunctionEntities.GetConfiguration' ---> DataAccess.. is where I built a model
cannot be resolved into a valid type or function.
虽然Get Configuration功能在我的控制器中。
我尝试过调试并得到同样的错误。我找不到如何解决此错误。
由于
调用DB函数(在Controller内部)给出错误: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);
}
路由指示请求正确的功能。这是控制器功能:
[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);
}
}
}
答案 0 :(得分:0)
我最近遇到了类似的问题。我的DBContext是使用EF6(数据库优先)导入数据库模型生成的。
我认为这个问题是由于代码生成后我已经根据需要重命名了DBContext类。项目重建成功但在运行时我得到了完全相同的错误:'MyDataContext.my_fn_name' cannot be resolved into a valid type or function. Near member access expression, line 1, column 23.
我再次重新生成了数据库模型,并在其他文件中调整了类名,现在一切正常。
答案 1 :(得分:0)
在我的情况下,我正在将一个函数从一个DBContext对象迁移到另一个DBContext对象,但由于缺少该行而出现此错误'MyDataContext.my_fn_name' cannot be resolved into a valid type or function
modelBuilder.Conventions.Add(new FunctionsConvention<MyDataContext>("schemaName"));
在OnModelCreating
中。
答案 2 :(得分:0)
您还记得为结果对象注册复杂类型吗?
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属性添加到您的结果类
[ComplexType]
public class GetConfiguration_Result
{
//..
}
此外,您的函数声明将需要指定名称空间
[Function(FunctionType.TableValuedFunction, "GetConfiguration", namespaceName : "xxxxxYourEntitiesxxxxx", Schema = "dbo")]
public virtual IQueryable<GetConfiguration_Result> GetConfiguration(string partialPIDs)