我目前正在使用 MVC 4 WebAPI w Entity Framework 数据库首先建模的场景。在我的 apicontroller 中,它给了我一个错误:
发生了错误。无法将类型为“WhereSelectEnumerableIterator
2[VB$AnonymousType_4
2[System.Nullable1[System.Guid],System.Collections.Generic.IEnumerable
1[CK5.Airline]],VB$AnonymousType_51[System.Nullable
1[System.Guid]]]”的对象转换为类型“System.Collections.Generic.IEnumerable1[CK5.Airline]'. </ExceptionMessage> <ExceptionType>System.InvalidCastException</ExceptionType> <StackTrace> at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func
1 func,CancellationToken cancelToken)
Public Class AirlineController
Inherits ApiController
' GET api/Airline
Function GetAirlines() As IEnumerable(Of Airline)
Using db As New DefaultEntities
Return From p In db.Airlines.AsEnumerable Order By {p.Name} Group By ParentID = p.ParentID Into parentIDGroup = Group Select New With {.ParentID = ParentID}
End Using
End Function
End Class
在我的实体模型对象中,ParentID 是一个可以为空的(guid)类型,我相信会导致问题。在使用 Linq2Sql 场景之前,我已经完成了这项工作,但是随着更新,这给了我一个问题。我不认为这是 web api 结构的问题,只是 w 实体框架。我哪里错了?