Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Find method of Entity Framework work?

I am learning Entity Framework and faced some moment with Find() method I can't understand.
Sample taken from book of Julia Lerman "Programming Entity Framework : Code First"

public class Destination
{
    public int DestinationId { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
    public string Description { get; set; }
    public byte?[]  Photo { get; set; }
    public ICollection<Lodging> Lodgings { get; set; }

    public Destination()
    {
        Lodgings = new List<Lodging>();
    }
}

public class Lodging
{
    public int LodgingId { get; set; }
    public string Name { get; set; }
    public string Owner { get; set; }
    public bool IsResort { get; set; }
    public Destination Destination { get; set; }
}

And I operate with the data in the following ways :

var destination = organizationDbContext.Destinations // case # 1
                .Include("Lodgings")
                .First(p=>p.DestinationId==1); 

var destination = organizationDbContext.Destinations.Find(1); // case # 2
  1. Why I can't call Find() method in the first case after Include() call, but can use Where() and First()?
  2. If I use second case with Find(), here I can't call Include() method to Lodgings, so how should I load related lodgings?

My questions could be expressed in another way:

  1. What is the right way to do : find one object and load all related inner objects (one-to-many)?
  2. What is the right way to do : load all objects (set A) and inner related objects (set A.I) , then find one by id from (A)?
like image 684
Iskander Raimbaev Avatar asked Sep 21 '16 18:09

Iskander Raimbaev


People also ask

Which approach is best in Entity Framework?

As in this diagram, if we already have domain classes, the Code First approach is best suited for our application. The same as if we have a database, Database First is a good option. If we don't have model classes and a database and require a visual entity designer tool then Model First is best suited.

How do I find my primary key EF?

EF Core Find method finds a record with the given primary key values. If the entity is already in the context (because of a previous query), then the Find method returns it. The Find method sends the query to the database if it does not find it in the context.

What is DbSet in Entity Framework?

A DbSet represents the collection of all entities in the context, or that can be queried from the database, of a given type. DbSet objects are created from a DbContext using the DbContext. Set method.


1 Answers

The point is that Find starts by searching in the local cache of the context. If no match are found then it sends a query to the db.

The Find method on DbSet uses the primary key value to attempt to find an entity tracked by the context. If the entity is not found in the context then a query will be sent to the database to find the entity there. Null is returned if the entity is not found in the context or in the database.

I think it's the inner explanation that there is no Find on IQueryable. When you are using the following code EF send always a request to the db:

var destination = organizationDbContext.Destinations // case # 1
                .Include("Lodgings")
                .First(p=>p.DestinationId==1); 

More infos : https://msdn.microsoft.com/en-us/data/jj573936.aspx

like image 69
Quentin Roger Avatar answered Sep 29 '22 04:09

Quentin Roger