Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve an object from entityframework without ONE field

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'm using SQL request to manage the "Data" part, but EF for the rest(metadata of the file).

I've one code which has to get all files id, filename, guid, modification date, ... of a file. This doesn't need at all the "Data" field.

Is there a way to retrieve a List but without this column filled?

Something like

context.Files.Where(f=>f.xyz).Exclude(f=>f.Data).ToList(); 

??

I know I can create anonymous objects, but I need to transmit the result to a method, so no anonymous methods. And I don't want to put this in a list of anonymous type, and then create a list of my non-anonymous type(File).

The goal is to avoid this:

using(RsSolutionsEntities context = new RsSolutionsEntities()) {     var file = context.Files         .Where(f => f.Id == idFile)         .Select(f => new {             f.Id, f.MimeType, f.Size, f.FileName, f.DataType,             f.DateModification, f.FileId         }).FirstOrDefault();      return new File() {         DataType = file.DataType, DateModification = file.DateModification,         FileId = file.FileId, FileName = file.FileName, Id = file.Id,         MimeType = file.MimeType, Size = file.Size     }; } 

(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.)

(e.g. this code throw the previous exception:

File file2 = context.Files.Where(f => f.Id == idFile)   .Select(f => new File() {Id = f.Id, DataType = f.DataType}).FirstOrDefault(); 

and "File" is the type I get with a context.Files.ToList(). This is the good class:

using File = MyProjectNamespace.Common.Data.DataModel.File; 

File is a known class of my EF datacontext:

public ObjectSet<File> Files {     get { return _files  ?? (_files = CreateObjectSet<File>("Files")); } } private ObjectSet<File> _files; 
like image 527
J4N Avatar asked Jan 23 '12 14:01

J4N


People also ask

What is include in EntityFramework?

Entity Framework Classic Include The Include method lets you add related entities to the query result. In EF Classic, the Include method no longer returns an IQueryable but instead an IncludeDbQuery that allows you to chain multiple related objects to the query result by using the AlsoInclude and ThenInclude methods.

What is the change tracker in EntityFramework?

The Change Tracking tracks changes while adding new record(s) to the entity collection, modifying or removing existing entities. Then all the changes are kept by the DbContext level. These track changes are lost if they are not saved before the DbContext object is destroyed.


1 Answers

Is there a way to retrieve a List but without this column filled?

Not without projection which you want to avoid. If the column is mapped it is natural part of your entity. Entity without this column is not complete - it is different data set = projection.

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.

As exception says you cannot project to mapped entity. I mentioned reason above - projection make different data set and EF don't like "partial entities".

Error 16 Error 3023: Problem in mapping fragments starting at line 2717:Column Files.Data in table Files must be mapped: It has no default value and is not nullable.

It is not enough to delete property from designer. You must open EDMX as XML and delete column from SSDL as well which will make your model very fragile (each update from database will put your column back). If you don't want to map the column you should use database view without the column and map the view instead of the table but you will not be able to insert data.

As a workaround to all your problems use table splitting and separate the problematic binary column to another entity with 1 : 1 relation to your main File entity.

like image 181
Ladislav Mrnka Avatar answered Sep 23 '22 12:09

Ladislav Mrnka