Search code examples
entity-frameworkentity-framework-6

EF6 Code first - skip binary (or any other) columns during load()


I have the ReportingActivity entity class.

public class ReportingActivity
    {
        public int                      ReportingActivityID             { get; set; }
        public DateTime                 ReportingActivitySend           { get; set; }
        public string                   Remark                          { get; set; }
        public string                   SendersCSV                      { get; set; }
        public string                   MailSenderStatus                { get; set; }
        public long                     RptGenerationCostMiliseconds    { get; set; }
        public DateTime                 RptGeneratedDateTime            { get; set; }
        public string                   RptGeneratedByWinUser           { get; set; }
        public string                   RptGeneratedOnMachine           { get; set; }
        public Int64                    Run                             { get; set; }
        public byte[]                   AttachmentFile                  { get; set; }

        public virtual Report           Report                          { get; set; }
        public virtual Employee         Employee                        { get; set; }
        public virtual ReportingTask    ReportingTask                   { get; set; }        
    }

I use this code to load data:

ctxDetail = new ReportingContext();
ctxDetail.ReportingActivity
      .Where(x => x.Employee.EmployeeID == currentEmployee.EmployeeID)
      .Load();

My code gets all the columns in (like SELECT * FROM... )

My question is how to skip the byte[] column, ideally recommend me a way how to improve my lines of code to be able specify exact list of columns.


Solution

  • Normally when dealing with a schema where records have large, seldom accessed details, it is beneficial to split those details off into a separate table as David mentions /w a one-to-one relationship back to the main record. These can be eager or lazy loaded as desired when they are needed.

    If changing the schema is not practical then another option when retrieving data from the table is to utilize Projection via Select to populate a view model containing just the fields you need, excluding the larger fields. This will help speed up things like reads for views, however for things like performing updates you will still need to load the entire entity including the large fields to ensure you don't accidentally overwrite/erase data. It is possible to perform updates without loading this data, but it will add a bit of complexity and risk of introducing bugs if mismanaged later.