Retrieve an object from entityframework without ONE field Retrieve an object from entityframework without ONE field sql sql

Retrieve an object from entityframework without ONE field


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.


I'd do something like this:

var result = from thing in dbContext.Things             select new Thing {                 PropertyA = thing.PropertyA,                 Another = thing.Another                 // and so on, skipping the VarBinary(MAX) property             };

Where Thing is your entity that EF knows how to materialize. The resulting SQL statement shouldn't include the large column in its result set, since it's not needed in the query.

EDIT: From your edits, you get the error NotSupportedException: The entity or complex type 'ProjectName.File' cannot be constructed in a LINQ to Entities query. because you haven't mapped that class as an entity. You can't include objects in LINQ to Entities queries that EF doesn't know about and expect it to generate appropriate SQL statements.

You can map another type that excludes the VarBinary(MAX) column in its definition or use the code above.


you can do this:

var files = dbContext.Database.SqlQuery<File>("select FileId, DataType, MimeType from Files");

or this:

var files = objectContext.ExecuteStoreQuery<File>("select FileId, DataType, MimeType from Files");

depending on your version of EF