x19
x19

Reputation: 8783

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

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; }

Upvotes: 2

Views: 10900

Answers (1)

t3chb0t
t3chb0t

Reputation: 18685

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();

Upvotes: 1

Related Questions