Retrieve an object from entityframework without ONE field

后端 未结 8 2189
[愿得一人]
[愿得一人] 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:42

    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.

    0 讨论(0)
  • 2020-11-30 03:43

    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.

    0 讨论(0)
提交回复
热议问题