Retrieve an object from entityframework without ONE field

后端 未结 8 2319
[愿得一人]
[愿得一人] 2020-11-30 02:54

I\'m using entity framework to connect with the database. I\'ve one little problem:

I\'ve one table which have one varbinary(MAX) column(with filestream).

I\

8条回答
  •  眼角桃花
    2020-11-30 03:30

    I'm using here the anonymous type because otherwise you will get a NotSupportedException: The entity or complex type 'ProjectName.File' cannot be constructed in a LINQ to Entities query.

    var file = context.Files
            .Where(f => f.Id == idFile)
            .FirstOrDefault() // You need to exeucte the query if you want to reuse the type
            .Select(f => new {
                f.Id, f.MimeType, f.Size, f.FileName, f.DataType,
                f.DateModification, f.FileId
            }).FirstOrDefault();
    

    And also its not a bad practice to de-normalize the table into further, i.e one with metadata and one with payload to avoid projection. Projection would work, the only issue is, need to edit any time a new column is added to the table.

提交回复
热议问题