An exception of type 'System.ArgumentException' occurred in EntityFramework.dll but was not handled in user code

c# entity-framework lambda linq

Question

I've faced a problem that is:

An exception of type 'System.ArgumentException' occurred in EntityFramework.dll but was not handled in user code

Additional information: The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.

How can I solve it?

This problem happen in FindAll method

articleViewModel.AttachmentFiles = AttachmentFileBLL.Instance.FindAll(c => c.ArticleId == articleViewModel.Id).ToList();

FindAll method:

public virtual IQueryable<TModel> FindAll(params Expression<Func<TModel, object>>[] includeProperties)
    {
        IQueryable<TModel> items = RepositoryContainer<TRepository>().FindAll();

        if (includeProperties != null)
        {
            foreach (var includeProperty in includeProperties)
            {
                items = items.Include(includeProperty); // Problem occurred here! 
            }
        }
        return items;
    }

Error location

public virtual int? ArticleId { get; set; }

public virtual int Id { get; set; }
1
2
1/26/2015 10:57:41 AM

Accepted Answer

You are passing an invalid argument to the DbExtensions.Include Method because it requires

A lambda expression representing the path to include.

and not a condition like you've specified:

c => c.ArticleId == articleViewModel.Id

You need to call FindAll differently:

AttachmentFileBLL
    .Instance
    .FindAll(c => c.ArticleId)
    .ToList();

This would specifiy the property.

But as you also need to specify a navigation property for this you need to use such a property there. I don't know what name it can have in you model but something like this perhaps:

AttachmentFileBLL
    .Instance
    .FindAll(c => c.Articles) // Assuming 'Articles' is a navigation property.
    .ToList();

If you want to get only some items you should put the condition elsewere in some Where that would suit your needs:

AttachmentFileBLL
   .Instance
   .SomeCollection
   .Where(c => c.ArticleId == articleViewModel.Id)
   .FindAll(c => c.Articles)
   .ToList();
1
1/27/2015 10:24:05 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow