{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/EntityFramework/issues/5632","repository_url":"https://api.github.com/repos/aspnet/EntityFramework","labels_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/5632/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/5632/comments","events_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/5632/events","html_url":"https://github.com/aspnet/EntityFramework/issues/5632","id":158241895,"number":5632,"title":"Entity Framework Core TPH inheritance causes ArguementNullException","user":{"login":"csmith93","id":19494864,"avatar_url":"https://avatars.githubusercontent.com/u/19494864?v=3","gravatar_id":"","url":"https://api.github.com/users/csmith93","html_url":"https://github.com/csmith93","followers_url":"https://api.github.com/users/csmith93/followers","following_url":"https://api.github.com/users/csmith93/following{/other_user}","gists_url":"https://api.github.com/users/csmith93/gists{/gist_id}","starred_url":"https://api.github.com/users/csmith93/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/csmith93/subscriptions","organizations_url":"https://api.github.com/users/csmith93/orgs","repos_url":"https://api.github.com/users/csmith93/repos","events_url":"https://api.github.com/users/csmith93/events{/privacy}","received_events_url":"https://api.github.com/users/csmith93/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/aspnet/EntityFramework/labels/investigation-needed","name":"investigation-needed","color":"0052cc"}],"state":"closed","locked":false,"assignee":{"login":"ajcvickers","id":1430078,"avatar_url":"https://avatars.githubusercontent.com/u/1430078?v=3","gravatar_id":"","url":"https://api.github.com/users/ajcvickers","html_url":"https://github.com/ajcvickers","followers_url":"https://api.github.com/users/ajcvickers/followers","following_url":"https://api.github.com/users/ajcvickers/following{/other_user}","gists_url":"https://api.github.com/users/ajcvickers/gists{/gist_id}","starred_url":"https://api.github.com/users/ajcvickers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ajcvickers/subscriptions","organizations_url":"https://api.github.com/users/ajcvickers/orgs","repos_url":"https://api.github.com/users/ajcvickers/repos","events_url":"https://api.github.com/users/ajcvickers/events{/privacy}","received_events_url":"https://api.github.com/users/ajcvickers/received_events","type":"User","site_admin":false},"milestone":null,"comments":4,"created_at":"2016-06-02T21:17:21Z","updated_at":"2016-06-09T14:41:10Z","closed_at":"2016-06-08T15:36:00Z","body":"### Steps to reproduce\r\n\r\nI have the following classes (abridged for brevity):\r\n\r\n    public interface IEntityModel\r\n    {\r\n        Guid Id { get; set; }\r\n    }\r\n\r\n    public abstract class User : IEntityModel {\r\n        public Guid Id { get; set; }\r\n        public int EmployeeId { get; set; }\r\n        public string Username { get; set; }\r\n        public bool IsManager { get; set; }\r\n        public virtual Manager Manager { get; set; }\r\n\r\n        // ...\r\n    }\r\n\r\n    public class Manager : User {\r\n        public virtual ICollection<User> Employees { get; set; }\r\n\r\n        // ...\r\n    }\r\n\r\n    public class Employee : User { \r\n\r\n        // ...\r\n    }\r\n\r\n    public class ApplicationDbContext : DbContext\r\n    {\r\n         private static bool _created = false;\r\n         public DbSet<User> Users { get; set; }\r\n         public DbSet<Schedule> Schedules { get; set; }\r\n         public DbSet<CellCarrierInfo> Carriers { get; set; }\r\n         public DbSet<Alert> Alerts { get; set; }\r\n         public DbSet<ValidationRule> Rules { get; set; }\r\n\r\n         public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)\r\n         {\r\n             if (!_created)\r\n             {\r\n                 _created = true;\r\n                 Database.EnsureCreated();\r\n            }\r\n        }\r\n\r\n        protected override void OnModelCreating(ModelBuilder modelBuilder)\r\n        {\r\n            // Users\r\n\r\n            modelBuilder.Entity<User>()\r\n                .HasDiscriminator<bool>(\"IsManager\")\r\n                .HasValue<Manager>(true)\r\n                .HasValue<Employee>(false);\r\n\r\n            // Alternate keys\r\n            modelBuilder.Entity<User>()\r\n                .HasAlternateKey(u => u.Username);\r\n            modelBuilder.Entity<User>()\r\n                .HasAlternateKey(u => u.EmployeeId);\r\n\r\n            // User will have a manager who will have employees\r\n            modelBuilder.Entity<User>()\r\n                .HasOne(u => u.Manager)\r\n                .WithMany(m => m.Employees);\r\n\r\n            // User will have schedules which will share an owner\r\n            modelBuilder.Entity<User>()\r\n                .HasMany(u => u.Schedules)\r\n                .WithOne(s => s.Owner);\r\n\r\n            // ...\r\n        }\r\n\r\n\r\n### The issue\r\n`User user = Context.Users.FirstOrDefault(u => u.Username == aUserName);` fails\r\n\r\n```\r\nSystem.ArgumentNullException: Value cannot be null.\r\nParameter name: constructor\r\n   at System.Linq.Expressions.Expression.New(ConstructorInfo constructor, IEnumerable`1 arguments)\r\n   at System.Linq.Expressions.Expression.New(ConstructorInfo constructor)\r\n   at Microsoft.Data.Entity.Metadata.Internal.EntityMaterializerSource.CreateMaterializeExpression(IEntityType entityType, Expression valueBufferExpression, Int32[] indexMap)\r\n   at Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.MaterializerFactory.CreateMaterializer(IEntityType entityType, SelectExpression selectExpression, Func`3 projectionAdder, IQuerySource querySource)\r\n   at Microsoft.Data.Entity.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitEntityQueryable(Type elementType)\r\n   at Microsoft.Data.Entity.Query.ExpressionVisitors.EntityQueryableExpressionVisitor.VisitConstant(ConstantExpression constantExpression)\r\n   at System.Linq.Expressions.ConstantExpression.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression expression)\r\n   at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.ReplaceClauseReferences(Expression expression, IQuerySource querySource, Boolean inProjection)\r\n   at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CompileMainFromClauseExpression(MainFromClause mainFromClause, QueryModel queryModel)\r\n   at Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.CompileMainFromClauseExpression(MainFromClause mainFromClause, QueryModel queryModel)\r\n   at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.VisitMainFromClause(MainFromClause fromClause, QueryModel queryModel)\r\n   at Remotion.Linq.Clauses.MainFromClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel)\r\n   at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)\r\n   at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)\r\n   at Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)\r\n   at Microsoft.Data.Entity.Query.Internal.SqlServerQueryModelVisitor.VisitQueryModel(QueryModel queryModel)\r\n   at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)\r\n   at Microsoft.Data.Entity.Storage.Database.CompileQuery[TResult](QueryModel queryModel)\r\n   at Microsoft.Data.Entity.Query.Internal.QueryCompiler.<>c__DisplayClass18_0`1.<CompileQuery>b__0()\r\n   at Microsoft.Data.Entity.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)\r\n   at Microsoft.Data.Entity.Query.Internal.QueryCompiler.CompileQuery[TResult](Expression query)\r\n   at Microsoft.Data.Entity.Query.Internal.QueryCompiler.Execute[TResult](Expression query)\r\n   at Microsoft.Data.Entity.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)\r\n   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)\r\n   at Persistence.Repositories.UserRepository.FindByUsername(String aUserName)\r\n   at Persistence.Repositories.UserRepository.FindOrCreate(String aUserName)\r\n   at TimeCardReminder.Controllers.UserController.Index()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n```\r\n\r\n### Further technical details\r\n\r\nEF Core version:\r\n    \r\n    \"EntityFramework.Core\": \"7.0.0-rc1-final\",\r\n    \"EntityFramework.MicrosoftSqlServer\": \"7.0.0-rc1-final\",\r\n    \"Microsoft.AspNet.Identity.EntityFramework\": \"3.0.0-rc1-final\",\r\n\r\nOperating system: Win7\r\nVisual Studio version: 2015\r\n\r\n\r\n[Issue taken from here for visibility](http://stackoverflow.com/q/37601377/6415214)."},"comment":{"url":"https://api.github.com/repos/aspnet/EntityFramework/issues/comments/224916183","html_url":"https://github.com/aspnet/EntityFramework/issues/5632#issuecomment-224916183","issue_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/5632","id":224916183,"user":{"login":"csmith93","id":19494864,"avatar_url":"https://avatars.githubusercontent.com/u/19494864?v=3","gravatar_id":"","url":"https://api.github.com/users/csmith93","html_url":"https://github.com/csmith93","followers_url":"https://api.github.com/users/csmith93/followers","following_url":"https://api.github.com/users/csmith93/following{/other_user}","gists_url":"https://api.github.com/users/csmith93/gists{/gist_id}","starred_url":"https://api.github.com/users/csmith93/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/csmith93/subscriptions","organizations_url":"https://api.github.com/users/csmith93/orgs","repos_url":"https://api.github.com/users/csmith93/repos","events_url":"https://api.github.com/users/csmith93/events{/privacy}","received_events_url":"https://api.github.com/users/csmith93/received_events","type":"User","site_admin":false},"created_at":"2016-06-09T14:41:09Z","updated_at":"2016-06-09T14:41:09Z","body":"@ajcvickers: I did find one thing interesting today with this. In:\r\n\r\n    modelBuilder.Entity<User>()\r\n        .HasDiscriminator<bool>(\"IsManager\")\r\n        .HasValue<Manager>(true)\r\n        .HasValue<Employee>(false);\r\n\r\nBecause even though `User` is an abstract class it'll still try to make `User` an option using the `.HasValue(\"User\")`, but that obviously won't work.\r\n\r\nAs a workaround I can use:\r\n\r\n    modelBuilder.Entity<User>()\r\n        .HasDiscriminator<bool?>(\"IsManager\")\r\n        .HasValue<User>(null)\r\n        .HasValue<Manager>(true)\r\n        .HasValue<Employee>(false);\r\n\r\nAnd then the migration ends up correct (it'll still have the discriminator as not null)."}}
{"actor":{"display_login":"csmith93"}}