Skip to content

Lazy Loading

Todd Thomson edited this page Sep 7, 2018 · 2 revisions

Lazy Loading

Entities.Sqlite provides lazy loading of entity relationship properties.

When defining your entities use:

  • EntityReference<TEntity> for HasOne or 1-One relationships
  • EntityCollection<TEntity> for HasMany or 1-Many relationships

Example Query

The following example show how to lazy load the Suppliers relationship property using .Value.

var query = from p in context.Products
            where p.Name == "Banana"
            select p;

var products = query.ToList<Product>();

Assert.Equal( "Bananas-R-US", products[ 0 ].Supplier.Value.Name );

Example Product Entity

    public class Product
    {
        public Product ()
        {
            Supplier = new EntityReference<Supplier>();
            Parts = new EntityCollection<Part>();
        }

        /// <summary>
        /// Gets or sets the order id. Primary key.
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// Gets or sets the product name.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the product price.
        /// </summary>
        public double Price { get; set; }

        /// <summary>
        /// Gets or sets the SupplierId. Foreign key. Required.
        /// </summary>
        public int SupplierId { get; set; }

        /// <summary>
        /// Gets or sets the supplier entity. HasOne, 1-1 relationship.
        /// </summary>
        public EntityReference<Supplier> Supplier { get; set; }

        /// <summary>
        /// Gets or sets the set of product parts. HasMany, 1-many relationship.
        /// </summary>
        public EntityCollection<Part> Parts { get; set; }
    }