Thursday, March 3, 2011

How to lazy-load a single property on a Linq entity?

I'm hand-coding Linq to SQL Entities and have a large XML column that I don't want loaded each time this entity is loaded, as it's infrequently used. Everywhere I search, I see that the Linq to SQL designer provides a "Delay Loaded" property, but the ColumnAttribute class doesn't have any hints as to how this is implemented.

I've gathered it's related to the System.Data.Linq.Link class, but I haven't found the magic incantation to make it work.

Does anyone know how to implement a lazy-loaded property in a hand-coded Linq to SQL C# class?

From stackoverflow
  • I've dissected the results of the designer's generated code.

    Step 1: Create a private member using the System.Data.Linq.Link type:

    private System.Data.Linq.Link<String> _columnName;
    

    Step 2: Set the Storage parameter of the Column attribute to this member name, and fill in the property getter and setters:

    [Column(Name = "column_name", Storage = "_columnName"...]
    public String ColumnName 
    {
        get
        {
            return _columnName.Value;
        }
        set
        {
            _columnName.Value = value;
        }
    }
    

0 comments:

Post a Comment