检索的EntityFramework对象无一领域 [英] Retrieve an object from entityframework without ONE field

查看:289
本文介绍了检索的EntityFramework对象无一领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架与数据库连接。我有一个小问题:

我有一个表其中有一个VARBINARY(MAX)列(含FILESTREAM)。

我使用的是SQL请求管理数据的一部分,但EF为休息(该文件的元数据)。

我有一个文件的一个code具有让所有的文件编号,文件名,GUID,修改日期...。这并不需要在所有的数据字段。

有没有一种方法来检索列表,但没有这一栏填?

类似于

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

??

我知道我可以创建匿名对象,但我需要的结果传递给方法,所以没有匿名方法。我不想把这个匿名类型的列表,然后创建我的非匿名类型(文件)的列表。

的目的是为了避免这样的:

 使用(RsSolutionsEntities上下文=新RsSolutionsEntities())
{
    var文件= context.Files
        。凡(F => f.Id == idFile)
        。选择(F =>新建{
            f.Id,f.MimeType,f.Size,f.FileName,f.DataType,
            f.DateModification,f.FileId
        })FirstOrDefault()。

    返回新的文件(){
        数据类型= file.DataType,DateModification = file.DateModification,
        FILEID = file.FileId,文件名= file.FileName,ID = file.Id,
        Mime类型= file.MimeType,大小= file.Size
    };
}
 

(我在这里使用匿名类型,因为否则你将得到一个NotSupportedException异常:实体或复杂类型ProjectName.File不能在LINQ到实体查询构造)

(如本code抛出previous异常:

 文件文件2 = context.Files.Where(F => f.Id == idFile)
  。选择(F =>新建文件(){n = f.Id,数据类型= f.DataType})。FirstOrDefault();
 

和文件是我与一个 context.Files.ToList()的类型。这是善类:

 使用File = MyProjectNamespace.Common.Data.DataModel.File;
 

文件是一个已知的类我的EF的DataContext的:

 公共对象集<文件>档
{
    {返回_FILES? (_ FILES = CreateObjectSet<文件>(文件)); }
}
私人对象集<文件> _FILES;
 

解决方案
  

有没有一种方法来检索列表,但没有这一栏填?

不是没有要避免投影。如果列映射这是你的实体的自然组成部分。实体没有此列是不完整的 - 它是不同的数据集=投影。

  

我在这里使用匿名类型,因为否则你将得到一个   NotSupportedException异常:实体或复杂类型ProjectName.File   不能在一个LINQ构造成实体查询。

作为例外,说,你不能预测到映射实体。我之所以提到以上​​ - 投影进行不同的数据集和EF不喜欢部分的实体。

  

错误16错误3023:映射碎片问题起始于行   2717:列Files.Data表文件必须映射:它没有   默认值,不能为空。

这是不够的,从设计中删除属性。您必须打开EDMX XML和从SSDL删除列,以及这将使你的模型很脆弱(从数据库每次更新都会把你列后)。如果你不想在列映射你应该使用数据库视图没有列和地图视图而不是表,但你将无法插入数据。

的解决方法是你所有的问题用<一个href="http://thedatafarm.com/blog/data-access/ef-table-splitting-ndash-the-opposite-of-entity-splitting/">table分裂并分离出有问题的二进制列与1另一个实体:1,相对于主文件实体

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;

解决方案

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.

这篇关于检索的EntityFramework对象无一领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆