Remove anonymous type info from Include exception message
I have the following model:
class Image
{
public int Id { get; set; }
public string Url { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
class Comment
{
public int Id { get; set; }
public string User { get; set; }
public string Text { get; set; }
}
class Post
{
public int Id { get; set; }
public string Heading { get; set; }
public string Text { get; set; }
public List<Image> Images { get; set; }
public List<Comment> Comments { get; set; }
}
class Blog
{
public int Id { get; set; }
public string Url { get; set; }
public string Name { get; set; }
public List<Post> Posts { get; set; }
}
class BloggingContext : DbContext
{
public DbSet<Image> Igames { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<Blog> Blogs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Filename=blogs.db");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
}
When I call:
using (BloggingContext db = new BloggingContext())
{
List<Blog> blogs = db.Blogs.Include(b => b.Posts).ThenInclude(p => new { p.Images, p.Comments }).ToList();
}
I get the following exception:
System.ArgumentException was unhandled
HResult=-2147024809
Message=The properties expression 'p => new <>f__AnonymousType4`2(Images = p.Images, Comments = p.Comments)' is not valid. The expression should represent a property access: 't => t.MyProperty'. When specifying multiple properties use an anonymous type: 't => new { t.MyProperty1, t.MyProperty2 }'.
Parameter name: propertyAccessExpression
ParamName=propertyAccessExpression
Source=EntityFramework.Core
StackTrace:
at System.Linq.Expressions.ExpressionExtensions.GetComplexPropertyAccess(LambdaExpression propertyAccessExpression)
at Microsoft.Data.Entity.Query.ResultOperators.Internal.ThenIncludeExpressionNode.ApplyNodeSpecificSemantics(QueryModel queryModel, ClauseGenerationContext clauseGenerationContext)
at Remotion.Linq.Parsing.Structure.IntermediateModel.MethodCallExpressionNodeBase.Apply(QueryModel queryModel, ClauseGenerationContext clauseGenerationContext)
at Remotion.Linq.Parsing.Structure.QueryParser.GetParsedQuery(Expression expressionTreeRoot)
at Microsoft.Data.Entity.Query.Internal.QueryCompiler.<>c__DisplayClass18_0`1.<CompileQuery>b__0()
at Microsoft.Data.Entity.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.Data.Entity.Query.Internal.QueryCompiler.CompileQuery[TResult](Expression query)
at Microsoft.Data.Entity.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.Data.Entity.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Remotion.Linq.QueryableBase`1.GetEnumerator()
at Microsoft.Data.Entity.EntityFrameworkQueryableExtensions.IncludableQueryable`2.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at EF7_Issue.Program.Main(String[] args) in c:\users\martin\documents\visual studio 2015\Projects\EF7-Issue\EF7-Issue\Program.cs:line 16
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
The exception itself suggests to use syntax like t => new { t.MyProperty1, t.MyProperty2 } which I am currently using.
I am using 7.0.0-rc1-final, .Net Framework 4.5.2 and Visual Studio 2015.
Looks like we are re-using a string when throwing from Include that does not make sense. Include does not support anonymous types like that.
Here is the correct syntax.
List<Blog> blogs = db.Blogs
.Include(b => b.Posts).ThenInclude(p => p.Comments)
.Include(b => b.Posts).ThenInclude(p => p.Images)
.ToList();
Leaving this issue open to address the issue with the exception message
@rowanmiller - What should be the exception message here?
We just need to remove the last part about using an anonymous type. Here is what it should be.
The property expression '{...}' is not valid. The expression should represent a property access: 't => t.MyProperty'.
BTW, no need to over-engineer this one - you can just copy/paste into a new resource. Trying to concatenate resources might sound like it saves duplicating work... but it turns out to be counter productive when it comes time to review/localize/etc.
@rowanmiller - It turns out that we used wrong exception message. We do have another exception message which is same as you suggested. Though Include & ThenInclude also allows property access like t => t.MyProperty.SubProperty. Should we have different message to indicate that or just use what you posted above?
On side note, we do not do any validation for the expression passed to Include & ThenInclude. And processing of both are done in bit different way.
If you specify incorrect expression in Include it throws
System.InvalidCastException : Unable to cast object of type 'System.Linq.Expressions.NewExpression' to type 'System.Linq.Expressions.MemberExpression'.
With ThenInclude it throws System.ArgumentException like the one in first post.
Though Include & ThenInclude also allows property access like t => t.MyProperty.SubProperty . Should we have different message to indicate that or just use what you posted above?
I probably wouldn't, it's kind of hard to cover all that in a single message.
After discussion, we decided to use a FWLink to point to the docs for Include (not yet written)
The property expression '{...}' is not valid. The expression should represent a property access: 't => t.MyProperty'. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.