I am quite new to Entity Framework, and while it has many advantages over NHibernate, I am disappointed to discover it does not support lazy loading of properties.
Take this class:
public class Product
{
public virtual Guid Id {get;set;}
public virtual string Name {get;set;}
public virtual string Details {get;set;}
}
My plan was to use Entity Splitting to map it to two tables:
CREATE TABLE [dbo].[Product](
[Id] [uniqueidentifier] NOT NULL PRIMARY KEY,
[Name] [nvarchar](50) NULL
)
CREATE TABLE [dbo].[ProductDetails](
[Id] [uniqueidentifier] NOT NULL PRIMARY KEY,
[Details] [nvarchar](max) NULL
)
And here is my fluent mapping:
modelBuilder.Entity<Product>()
.Map(m =>
{
m.Properties(t => new { t.Id, t.Name });
m.ToTable("Product");
})
.Map(m =>
{
m.Properties(t => new { t.Id, t.Details});
m.ToTable("ProductDetails");
});
I want to be able to show a list of products without loading the details field. However, whenever I load a product it always does an INNER JOIN. I want it to only read from Product, but then read from ProductDetails when I read the Details property.
How can this be achieved?
If it is not possible, how else can I implement lazy loading of properties?
Table Splitting is not acceptable as this means the persistence mechanism is dictating the design of the domain.
Edit after CodeCaster's answer:
The domain is fixed - I do not want a solution that introduces a ProductDetails entity. This question is about persisting an existing domain model. Altering the domain does not answer the question.
You can't, lazy-loading only works for navigation properties.
If you change your model like this:
You can utilize lazy loading, where
ProductDetails
only will be queried when you get theProduct.Details
property.Entity models don't have to be domain models.