{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/EntityFramework/issues/7070","repository_url":"https://api.github.com/repos/aspnet/EntityFramework","labels_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/7070/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/7070/comments","events_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/7070/events","html_url":"https://github.com/aspnet/EntityFramework/issues/7070","id":190537285,"number":7070,"title":"Bizarre InvalidCastException error","user":{"login":"jeffbromberger","id":6789497,"avatar_url":"https://avatars.githubusercontent.com/u/6789497?v=3","gravatar_id":"","url":"https://api.github.com/users/jeffbromberger","html_url":"https://github.com/jeffbromberger","followers_url":"https://api.github.com/users/jeffbromberger/followers","following_url":"https://api.github.com/users/jeffbromberger/following{/other_user}","gists_url":"https://api.github.com/users/jeffbromberger/gists{/gist_id}","starred_url":"https://api.github.com/users/jeffbromberger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jeffbromberger/subscriptions","organizations_url":"https://api.github.com/users/jeffbromberger/orgs","repos_url":"https://api.github.com/users/jeffbromberger/repos","events_url":"https://api.github.com/users/jeffbromberger/events{/privacy}","received_events_url":"https://api.github.com/users/jeffbromberger/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-11-20T03:37:18Z","updated_at":"2016-11-20T16:22:46Z","closed_at":null,"body":"I'm doing a query with some Include directives to pull in related entities and then build a list of a different type of object:\r\n\r\n `\r\n```\r\n    // this is the object I'm trying to build a list of...\r\n    public class AlertListDTO\r\n    {\r\n        public long Id { get; set; }\r\n        public string Field1 { get; set; }\r\n        public string Company { get; set; }\r\n        public string Hostname { get; set; }\r\n        public DateTime ReportedTime { get; set; }\r\n    }\r\n```\r\n`\r\n\r\nThis is the query that causes the exception... \r\n        \r\n```\r\n`                var alertList = databaseStore.Alerts\r\n                                .Include(a => a.Client)\r\n                                .Include(a => a.Entity)\r\n                                .Include(a => a.Company)\r\n                                .AsNoTracking()\r\n                                .Select(a => new AlertListDTO()\r\n                                {\r\n                                    Field1 = a.Entity.Field1,\r\n                                    Company = a.Company.Name,\r\n                                    Hostname = a.Client.Hostname,\r\n                                }).ToList();\r\n`\r\n\r\n```\r\nI have spent hours and hours trying to figure out what is wrong, as I am completely unable to decipher the exception stack shown below.  What I have found after a painful amount of trial and error, which makes no sense to me, is that if I change the lines:\r\n\r\n```\r\n                                    Field1 = a.Entity.Field1,\r\n                                    Company = a.Company.Name,\r\n\r\n```\r\nto\r\n\r\n```\r\n                                    Company = a.Company.Name,\r\n                                    Field1 = a.Entity.Field1,\r\n\r\n```\r\nthen everything works!  To be clear, all I am doing is changing the order of those two lines, no changes to the lines themselves.  This goes against everything I think I know about C#, programming, and life in general lol.  There *has* to be something else going on here but I am completely unable to see it.\r\n\r\n\r\n\r\n```\r\nException message:\r\nStack trace:\r\n{System.InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.Expression`1[System.Func`2[Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[CloudApi.Models.Alert,System.Collections.Generic.IEnumerable`1[CloudApi.Models.Client]],CloudApi.Models.Client],System.Int64]]' to type 'System.Linq.Expressions.ConstantExpression'.\r\n   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.QueryFlattener.Flatten(MethodCallExpression methodCallExpression)\r\n   at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.OptimizeJoinClause(JoinClause joinClause, QueryModel queryModel, Int32 index, Action baseVisitAction, MethodInfo operatorToFlatten, Boolean outerJoin)\r\n   at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitGroupJoinClause(GroupJoinClause groupJoinClause, QueryModel queryModel, Int32 index)\r\n   at Remotion.Linq.Clauses.GroupJoinClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel, Int32 index)\r\n   at Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection`1 bodyClauses, QueryModel queryModel)\r\n   at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)\r\n   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)\r\n   at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)\r\n   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)\r\n   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](QueryModel queryModel)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass19_0`1.<CompileQuery>b__0()\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQuery[TResult](Expression query)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)\r\n   at Remotion.Linq.QueryableBase`1.GetEnumerator()\r\n   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n   at CloudApi.Controllers.AlertsController.Index(Int32 version, String companyId) in AlertsController.cs:line 48}\r\n```\r\n\r\n### Further technical details\r\n\r\n```\r\n`{\r\n    \"dependencies\": {\r\n        \"Microsoft.AspNetCore.Server.IISIntegration\": \"1.0.0\",\r\n        \"Microsoft.Extensions.Configuration.EnvironmentVariables\": \"1.0.0\",\r\n        \"Microsoft.Extensions.Configuration.FileExtensions\": \"1.0.0\",\r\n        \"Microsoft.Extensions.Configuration.Json\": \"1.0.0\",\r\n        \"Microsoft.Extensions.Logging\": \"1.0.0\",\r\n        \"Microsoft.Extensions.Logging.Console\": \"1.0.0\",\r\n        \"Microsoft.Extensions.Logging.Debug\": \"1.0.0\",\r\n        \"Microsoft.Extensions.Options.ConfigurationExtensions\": \"1.0.0\",\r\n        \"Microsoft.ApplicationInsights.AspNetCore\": \"1.0.2\",\r\n        \"Microsoft.AspNetCore.Mvc\": \"1.0.1\",\r\n        \"Microsoft.AspNetCore.Server.Kestrel\": \"1.0.1\",\r\n        \"Microsoft.NETCore.App\": \"1.0.1\",\r\n        \"Microsoft.EntityFrameworkCore\": \"1.0.1\",\r\n        \"Microsoft.EntityFrameworkCore.Tools\": \"1.0.0-preview2-final\",\r\n        \"Microsoft.AspNetCore.Diagnostics\": \"1.0.0\",\r\n        \"System.IdentityModel.Tokens.Jwt\": \"5.0.1-pre-310121323\",\r\n        \"Microsoft.AspNetCore.Authentication.JwtBearer\": \"1.0.0\",\r\n        \"Microsoft.AspNetCore.Cors\": \"1.0.0\",\r\n        \"Microsoft.Extensions.Caching.Memory\": \"1.0.0\",\r\n        \"SapientGuardian.EntityFrameworkCore.MySql\": \"7.1.14\",\r\n        \"AWSSDK.Core\": \"3.3.5\",\r\n        \"AWSSDK.SimpleEmail\": \"3.3.1.1\",\r\n        \"AWSSDK.CognitoIdentity\": \"3.3.0.3\",\r\n        \"AWSSDK.CognitoIdentityProvider\": \"3.3.2\",\r\n        \"AWSSDK.Extensions.NETCore.Setup\": \"3.3.0.1\"\r\n    },\r\n\r\n    \"tools\": {\r\n        \"Microsoft.AspNetCore.Server.IISIntegration.Tools\": \"1.0.0-preview2-final\",\r\n        \"Microsoft.EntityFrameworkCore.Tools\": \"1.0.0-preview2-final\"\r\n    },\r\n\r\n    \"frameworks\": {\r\n        \"netcoreapp1.0\": {\r\n            \"imports\": [\r\n                \"dotnet5.6\",\r\n                \"portable-net45+win8\"\r\n            ]\r\n        }\r\n    },\r\n\r\n    \"runtimes\": {\r\n        \"win10-x64\": { }\r\n    },\r\n\r\n  \"buildOptions\": {\r\n    \"emitEntryPoint\": true,\r\n    \"preserveCompilationContext\": true\r\n  },\r\n\r\n  \"runtimeOptions\": {\r\n    \"configProperties\": {\r\n      \"System.GC.Server\": true\r\n    }\r\n  },\r\n\r\n  \"publishOptions\": {\r\n    \"include\": [\r\n      \"wwwroot\",\r\n      \"appsettings.json\",\r\n      \"appsettings.production.json\",\r\n      \"web.config\"\r\n    ]\r\n  },\r\n\r\n  \"scripts\": {\r\n    \"postpublish\": [ \"dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%\" ]\r\n  }\r\n}\r\n`\r\n\r\n```\r\nVisual Studio 2015 Pro Update 3\r\nWindows 10 x64\r\n\r\nAll models and database context config can be found here:\r\n\r\nhttps://dl.dropboxusercontent.com/u/32742882/cast-exception.zip\r\n"},"comment":{"url":"https://api.github.com/repos/aspnet/EntityFramework/issues/comments/261787856","html_url":"https://github.com/aspnet/EntityFramework/issues/7070#issuecomment-261787856","issue_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/7070","id":261787856,"user":{"login":"jeffbromberger","id":6789497,"avatar_url":"https://avatars.githubusercontent.com/u/6789497?v=3","gravatar_id":"","url":"https://api.github.com/users/jeffbromberger","html_url":"https://github.com/jeffbromberger","followers_url":"https://api.github.com/users/jeffbromberger/followers","following_url":"https://api.github.com/users/jeffbromberger/following{/other_user}","gists_url":"https://api.github.com/users/jeffbromberger/gists{/gist_id}","starred_url":"https://api.github.com/users/jeffbromberger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jeffbromberger/subscriptions","organizations_url":"https://api.github.com/users/jeffbromberger/orgs","repos_url":"https://api.github.com/users/jeffbromberger/repos","events_url":"https://api.github.com/users/jeffbromberger/events{/privacy}","received_events_url":"https://api.github.com/users/jeffbromberger/received_events","type":"User","site_admin":false},"created_at":"2016-11-20T16:22:46Z","updated_at":"2016-11-20T16:22:46Z","body":"@maumar  I have simplified the models as much as I could to still repro the issue.  I updated the first post with the new naming and also a link to a zip with the model files and the database config routine."}}
{"actor":{"display_login":"jeffbromberger"}}