Hello,
Im trying to port an old ASP.NET WebAPI project to .NET Core. (The code works with EF 6)
While doing so I encountered an error with one of my Linq-queries.
Im not really sure what SQL is generated from the query but it seems to be wrong since the error doesn't make any sense to me.
Models
public class Node
{
public Node()
{
NodeRecords = new List<NodeRecord>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string Name { get; set; }
public virtual List<NodeRecord> NodeRecords { get; set; }
}
public class NodeRecord
{
[Key]
public int NodeRecordId { get; set; }
[Column("Node_Name")]
[ForeignKey("Node_Name")]
public string NodeName { get; set; }
public int Change { get; set; }
}
public class HighNode
{
public string Name { get; set; }
public int Change { get; set; }
}
Code
private List<HighNode> GetHighNodeDaysAgo(int days)
{
List<HighNode> selection = new List<HighNode>();
using (GameStatsDb db = new GameStatsDb())
{
selection = db.Nodes.Include(x => x.NodeRecords)
.Select(s => new HighNode()
{
Name = s.Name,
Change = s.NodeRecords.OrderByDescending(o => o.NodeRecordId).Skip(days).FirstOrDefault().Change
}).OrderByDescending(t => t.Change).Take(100).ToList();
}
return selection;
}
Error Information:
An exception of type 'System.InvalidCastException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code
Additional information: Unable to cast object of type 'System.Int32' to type 'GameDbModels.NodeRecord'.
StackTrace
at lambda_method(Closure , ValueBuffer )
at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at GameStatsServiceCrossPlatform.Controllers.NodeController.GetHighNodeDaysAgo(Int32 days) in ~\Controllers\NodeController.cs:line 82
at GameStatsServiceCrossPlatform.Controllers.NodeController.GetTodaysHighNodes() in ~\Controllers\NodeController.cs:line 33
at lambda_method(Closure , Object , Object[] )
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionFilterAsync>d__28.MoveNext()
Further technical details
Visual Studio version and tool:
Running Microsoft .NET Core Tools (Preview 2) in VS 2015 update 3
OS:
Windows 10
Framework versions:
"Microsoft.EntityFrameworkCore": "1.0.0",
"Microsoft.EntityFrameworkCore.Relational": "1.0.0",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
Any idea what could be the issue?
I haven't seen that Take & Skip aren't supported yet and I have seen examples containing orderby.
Hello,
Im trying to port an old ASP.NET WebAPI project to .NET Core. (The code works with EF 6)
While doing so I encountered an error with one of my Linq-queries.
Im not really sure what SQL is generated from the query but it seems to be wrong since the error doesn't make any sense to me.
Models
Code
Error Information:
An exception of type 'System.InvalidCastException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code
Additional information: Unable to cast object of type 'System.Int32' to type 'GameDbModels.NodeRecord'.
StackTrace
Further technical details
Visual Studio version and tool:
Running Microsoft .NET Core Tools (Preview 2) in VS 2015 update 3
OS:
Windows 10
Framework versions:
"Microsoft.EntityFrameworkCore": "1.0.0",
"Microsoft.EntityFrameworkCore.Relational": "1.0.0",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
Any idea what could be the issue?
I haven't seen that Take & Skip aren't supported yet and I have seen examples containing orderby.