efcore: Include with a mapped query view with invalid navigation name fails with null object from internal code
I’ve mapped a DbQuery of a non-entity type to a database view. (The true type and db view are named AuthorArticlesCount. I’m calling them AuthorStat here to simplify but the repo I’m linking to uses the longer name.)
public DbQuery<AuthorArticleCount> AuthorArticleCounts{get;set;}
I can run simple LINQ queries successfully.
I then mapped that type as a dependent in a one to one relationship with an entity.
modelBuilder.Query<AuthStat>().HasOne<Author>()
.WithOne()
.HasForeignKey<AuthorStat>(a=>a.AuthorId);
Note that there are no navigation properties in Author or AuthorStat. AuthorStat has only AuthorId FK property.
Queries against the query type that include the entity fail. Queries against the entity that include the query type fail. e.g.
` var results = _context.Authors.Include("AuthorArticleCounts").ToList();
`
The failure is a null reference exception coming from
Microsoft.EntityFrameworkCore.Metadata.Internal.EntityTypeExtensions.DisplayName(IEntityType type)
Full repo: https://github.com/julielerman/PublicationsTracker
Further technical details
EF Core version:2.1.0-rc1-final Database Provider: Microsoft.EntityFrameworkCore.Sqlite) Operating system: macOS High Sierra IDE: VS Code 1.21.1
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 19 (7 by maintainers)
Commits related to this issue
- Update query types topic to clarify navigations See https://github.com/aspnet/EntityFrameworkCore/issues/11971 for some context on what needs clarification. — committed to dotnet/EntityFramework.Docs by divega 6 years ago
- Update query types topic to clarify navigations (#698) * Update query types topic to clarify navigations See https://github.com/aspnet/EntityFrameworkCore/issues/11971 for some context on what nee... — committed to dotnet/EntityFramework.Docs by divega 6 years ago
- Fix #11971 - Fix error handling for bad nav prop binding. — committed to dotnet/efcore by anpete 6 years ago
- Fix #11971 - Fix error handling for bad nav prop binding. — committed to dotnet/efcore by anpete 6 years ago
I have encountered this error and I think the exception message could be more informative than
NullReferenceException
.In my case I did
.Include(message => message.UserId)
instead of.Include(message => message.User)
(included foreign key instead of navigational property). I spend good 20 minutes tracking this simple problem.