c# – Entity Framework – Data Access Tips

Question:

I'm working on a project using EF6 with C#, in one of my classes I have to keep a file, example below:

   public partial class Arquivo
    {
        [Key]
        public int ArquivoID { get; set; }

        public string Nome { get; set; }
        public byte[] ArquivoFisico { get; set; }
        public string TipoArquivo { get; set; }
     }

my problem is in the arquivofisico property, when I consult the data of other classes that are linked to this one, I would not like it to load the arquivofisico property, because I can have files of 5, 10 MB, my server's memory will overflow if I have many classes.. I want to access this property only when using a generic download handler . Do you have any tips to do this or do I have to use lazy loading?

Thank you for your help.

Answer:

Do you have any tips to do this or do I have to use lazy loading?

Do not materialize the column when selecting. Do as follows:

var arquivos = db.Arquivos
                 .Where(...)
                 .Select(a => new { 
                                    a.ArquivoID, 
                                    a.Nome, 
                                    a.TipoArquivo })
                 .ToList();
Scroll to Top