从没有一个字段的实体框架中检索对象 [英] Retrieve an object from entityframework without ONE field

查看:34
本文介绍了从没有一个字段的实体框架中检索对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体框架来连接数据库.我有一个小问题:

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

我有一张表,其中有一个 varbinary(MAX) 列(带有文件流).

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

我使用 SQL 请求来管理数据"部分,但使用 EF 来管理其余部分(文件的元数据).

I'm using SQL request to manage the "Data" part, but EF for the rest(metadata of the file).

我有一个代码,它必须获取文件的所有文件 id、文件名、guid、修改日期…….这根本不需要数据"字段.

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?

类似的东西

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
    };
}

(我在此处使用匿名类型,否则您将收到 NotSupportedException:无法在 LINQ to Entities 查询中构造实体或复杂类型ProjectName.File".)

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

和文件"是我通过 context.Files.ToList() 得到的类型.这是好课:

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

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

文件是我的 EF 数据上下文的已知类:

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.

我在这里使用匿名类型,否则你会得到一个NotSupportedException: 实体或复杂类型ProjectName.File"不能在 LINQ to Entities 查询中构造.

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.

作为例外,您不能投影到映射实体.我上面提到了原因 - 投影制作不同的数据集,EF 不喜欢部分实体".

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".

错误 16 错误 3023:从行开始映射片段的问题2717:列文件.必须映射表文件中的数据:它没有默认值且不可为空.

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.

从设计器中删除属性是不够的.您必须以 XML 格式打开 EDMX 并从 SSDL 中删除列,这将使您的模型非常脆弱(数据库的每次更新都会使您的列恢复原状).如果您不想映射列,则应使用没有列的数据库视图并映射视图而不是表,但您将无法插入数据.

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.

作为解决所有问题的方法,请使用表格拆分 并将有问题的二进制列与另一个与您的主 File 实体具有 1:1 关系的实体分开.

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.

这篇关于从没有一个字段的实体框架中检索对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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