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 had the following issue:

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 do I fix it?

This issue started inFindAll method

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

FindAll approach:

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 sending the Method DbExtensions.Include an incorrect argument since it requires

A lambda expression representing the path to include.

and not a must as you've described:

c => c.ArticleId == articleViewModel.Id

Please make a call.FindAll differently:

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

The property would be specified in this way.

However, you must use a navigation property there since you must also define one for this. I'm not sure what name it may go by in your model, but perhaps something like this:

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

Put the other conditions in some of the things you want to purchase only.Where that would be appropriate for you

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