我正在使用ASP.NET Web API 2和Entity Framework开发Web API来访问数据库。我调用我的SQL Server存储过程非常简单,应按如下方式返回一列:
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[getnationalities]
AS
BEGIN
SET NOCOUNT ON;
SELECT DISTINCT
Table_Price.Price_Nationality AS 'name'
FROM
Table_Price;
END
这是我的vb代码:
Namespace Controllers
Public Class NationalityController
Inherits ApiController
Public Function getcountries() As IHttpActionResult
Using entities As IAPD_DBEntities = New IAPD_DBEntities()
Return Ok(entities.getnationalities.ToList)
End Using
End Function
End Class
End Namespace
这是我从邮递员那里得到的错误
{
"message": "An error has occurred.",
"exceptionMessage": "The data reader is incompatible with the specified 'IAPD_DBModel.Table_Price'. A member of the type, 'Price_PK_ID', does not have a
corresponding column in the data reader with the same name.",
"exceptionType": "System.Data.Entity.Core.EntityCommandExecutionException",
"stackTrace": " at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.GetMemberOrdinalFromReader(DbDataReader storeDataReader, EdmMember member, EdmType currentType, Dictionary`2 renameList)\r\n at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.GetColumnMapsForType(DbDataReader storeDataReader, EdmType edmType, Dictionary`2 renameList)\r\n at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.CreateColumnMapFromReaderAndType(DbDataReader storeDataReader, EdmType edmType, EntitySet entitySet, Dictionary`2 renameList)\r\n at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.CreateFunctionImportStructuralTypeColumnMap(DbDataReader storeDataReader, FunctionImportMappingNonComposable mapping, Int32 resultSetIndex, EntitySet entitySet, StructuralType baseStructuralType)\r\n at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.FunctionColumnMapGenerator.System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.IColumnMapGenerator.CreateColumnMap(DbDataReader reader)\r\n at System.Data.Entity.Core.Objects.ObjectContext.MaterializedDataRecord[TElement](EntityCommand entityCommand, DbDataReader storeReader, Int32 resultSetIndex, ReadOnlyCollection`1 entitySets, EdmType[] edmTypes, ShaperFactory`1 shaperFactory, MergeOption mergeOption, Boolean streaming)\r\n at System.Data.Entity.Core.Objects.ObjectContext.CreateFunctionObjectResult[TElement](EntityCommand entityCommand, ReadOnlyCollection`1 entitySets, EdmType[] edmTypes, ExecutionOptions executionOptions)\r\n at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass47`1.<ExecuteFunction>b__46()\r\n at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)\r\n at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass47`1.<ExecuteFunction>b__45()\r\n at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)\r\n at System.Data.Entity.Core.Objects.ObjectContext.ExecuteFunction[TElement](String functionName, ExecutionOptions executionOptions, ObjectParameter[] parameters)\r\n at ProjectDataAccess.IAPD_DBEntities.getnationalities() in C:\\Users\\Junaida\\documents\\visual studio 2015\\Projects\\WebApplication7\\ProjectDataAccess\\DataAccessModel.Context.vb:line 5552\r\n at WebApplication7.Controllers.NationalityController.getcountries() in C:\\Users\\Junaida\\documents\\visual studio 2015\\Projects\\WebApplication7\\WebApplication7\\Controllers\\NationalityController.vb:line 13\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
}
我真正的目标是获得一个数据集或一系列国籍
答案 0 :(得分:0)
抱歉,我不知道实体框架,但我可以从存储过程中获取数据列表。
Dim lstNationalities As New List(Of String)
Using cn As New SqlClient.SqlConnection("Your connection string")
Using cmd As New SqlClient.SqlCommand With {
.Connection = cn,
.CommandType = CommandType.StoredProcedure,
.CommandText = "getnationalities"}
cn.Open
Using dr As SqlClient.SqlDataReader = cmd.ExecuteReader
Do While dr.Read
lstNationalities.Add(dr.GetString(0))
Loop
End Using
End Using
End Using