Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove anonymous type info from Include exception message #4058

Closed
mkucera47 opened this issue Dec 12, 2015 · 6 comments
Closed

Remove anonymous type info from Include exception message #4058

mkucera47 opened this issue Dec 12, 2015 · 6 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@mkucera47
Copy link

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.

@rowanmiller
Copy link
Contributor

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 rowanmiller changed the title ArgumentException on ThenInclude with multiple properties Remove anonymous type info from Include exception message Dec 15, 2015
@rowanmiller rowanmiller added this to the 7.0.0 milestone Dec 15, 2015
@smitpatel
Copy link
Contributor

@rowanmiller - What should be the exception message here?

@rowanmiller
Copy link
Contributor

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.

@smitpatel
Copy link
Contributor

@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.

@rowanmiller
Copy link
Contributor

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.

@rowanmiller
Copy link
Contributor

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.

@smitpatel smitpatel modified the milestones: 1.0.0-rc2, 1.0.0 Mar 8, 2016
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
@ajcvickers ajcvickers modified the milestones: 1.0.0-rc2, 1.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

4 participants