I cannot use the Find() method using Entityframework

Asked

Viewed 363 times

1

I’m using the following tutorial to use the Repository Pattern:

Follows my repository class

public class Repository<T> : IDisposable, IRepository<T> where T : class
{
    protected readonly EntityContext ctx;

    public Repository(EntityContext _ctx)
    {
        this.ctx = _ctx;
    }

    public T getByID(object id)
    {
        return ctx.Set<T>().Find(id);
    }
}

But at the time of the creation of the method GetByID(int id), is popping the error below.

Dbset does not contain a Definition for 'Find' and no Extension method 'Find' Accepting a first argument of type 'DbSet<T>' could be found (are you Missing a Directive or an Assembly Reference?)

In the tutorial I’m following, the instructor teaches the same way I showed here.

  • Are you using using System.Collections.Generic ?

  • Yeah, I am. Yeah; using System.Collections.Generic;

  • using System.Linq

  • What version of . NET? What version of the Entity Framework?

  • Try swapping Entitycontext for Dbcontext

  • Is that Entityframework Core? Where did this Entitycontext class come from?

  • @bunomonteiro the version of . NET is .NET Framework 4.7.1 and the version of the Entity Framework I used at the time of installation via Nuget was as follows: -Version 7.0.0-rc1-final -Pre

  • @LINQ It’s not Core... And the entitycontext class is the context of apication, where I have declared the classes that will be reflected as a database.

  • @Renannarciso Please add the relevant snippets to your question and not to external sources. My network is blocked here.

  • @bunomonteiro, I switched Entitycontext for Dbcontext and still I can’t call the Find method()

  • Dude, I think it’s because the find needs a lambda expression, so it would have to be like Find(x => x.valor == id)

  • 1

    Installs -Version 6.2.0 which is stable.

  • @Gustavoforteandreli I know I need a lambda expression. The problem is I’m not able to use it

  • Have you tried changing the method public T getByID<T>(object id)?

  • Hello @Virgilionovic the problem has already been solved

  • You can put the solution.

  • Yes, thanks for the info. Solution.

Show 12 more comments

3 answers

1


Problem solved, I installed Entityframework 6.2.0 and got it.

0

I installed Entityframework 6.2.0 as the @Brunomoner commented and solved the problem.

0

Good afternoon!

Check that the context being passed to the repository is correct. I’ve used this format before, and what you’re presenting should work. Apparently you have a problem in the instance of your context.

But as a rule I don’t like to use Find, because he has two problems.

1) If you are using Lazy Loading, and your object has many subobjects or EF relacinment lists, if you browse them, EF will do several trips to your database, which is a performance failure of your application.

2) If you are not using Lazy Loading, if you need this data from the sub-targets, you will not be able to access it.

The ideal would be for you to use

context.Set<TEntity>().Include().Where() 

Includes is for accessing the subobjects, but the repository methodology is not the most suitable for this.

Deploying Repository for EF is not advisable.

This site has a very complete documentation about EF. I suggest that you read carefully so that you create an infrastructure appropriate to your need.

http://www.entityframeworktutorial.net/efcore/querying-in-ef-core.aspx

  • Hmmm, interesting. But kind of the idea I want to use the generic Repository is so I don’t create multiple Daos understood? I want a repository to fit all business classes. - E why It is not advisable to use Repository for EF?

  • Understood, however the EF was created to extinguish the need for a repository, you can create microservices for each crud action, and use them in their opportune moments. EX: To add you would create a class called Adder, and in the controller you would instantiate Adder. Adder can be generic just like the repository but with only one responsibility. In my view the Repository Standard hurts the principle of Sole Responsibility. Not to mention that in the repository pattern it is impossible for you to make those considerations about getter.

  • @RenanNarciso https://answall.com/questions/51536/quando-usar-entity-framework-com-repository-pattern

Browser other questions tagged

You are not signed in. Login or sign up in order to post.