I can not use the Find () method using EntityFramework

1

I'm using the next tutorial to use the Repository Pattern:

Follow 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 creating the GetByID(int id) method, the error below is popping up.

  

DbSet does not contain a definition for 'Find' and in the extension method 'Find' accepting the 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.

    
asked by anonymous 06.03.2018 / 20:58

2 answers

1

Problem solved, I installed EntityFramework 6.2.0 and succeeded.

    
20.03.2018 / 17:13
0

Good afternoon!

Verify that the context being passed to the repository is correct. I have already used this format, and what you are presenting should work. Apparently you have a problem in the instance of your context.

But as a rule I do not like to use Find because it has two problems.

1) If you are using Lazy Loading, and your object has many subobjects or EF relational lists, if you navigate through them, EF will make a number of trips to your database, which is a performance failure. your application.

2) If you do not have Lazy Loading, if you need this subobject data, you will not be able to access it.

The Ideal would be you to use

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

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

Implementing Repository for EF is not advisable.

This site has a very thorough documentation on EF. I suggest you read carefully so that you can create an infrastructure that fits your needs.

link

    
07.03.2018 / 18:59