简体   繁体   English

如何使用Entity Framework 5从LINQ查询的结果中省略一列

[英]How do omit a column(s) from the results of a LINQ query using Entity Framework 5

I have image data stored in a database. 我将图像数据存储在数据库中。 In addition to the image data there are fields which include meta information (image_size, description, name, etc). 除图像数据外,还有包含元信息(image_size,描述,名称等)的字段。 I would like to return only the meta information for the image and not the actual image byte data itself. 我只想返回图像的元信息,而不返回实际的图像字节数据本身。 I am hoping that there is a way to return all fields EXCEPT FOR the image data without having to select each column individually into an anonymous object . 我希望有一种方法可以返回除图像数据以外的所有字段,而不必将每个列分别选择为一个匿名对象 I am using Entity Framework 5.0 and Code First, so no designers or EDMX's or anything. 我使用的是Entity Framework 5.0和Code First,所以没有设计器或EDMX或其他任何东西。

I saw this post ... selecting-all-but-one-property-using-linq-entity-framework ... but the answer is focused on a solution using the designer and my code is all code first with no designer involved. 我看到了这篇文章... 使用linq-entity-framework选择所有但一个属性 ...但是答案集中在使用设计器的解决方案上,我的代码首先是所有代码,而没有涉及设计器。 Also when I did a search on "deferring" individual columns I am directed to a page on MS site that deals with deferring loading on the class instead of the individual fields, besides it says it's information is out of date (presumably 5.0 does it differently?). 同样,当我搜索“延迟”单个列时,我被定向到MS网站上的一个页面 ,该页面处理延迟类的加载而不是单个字段,此外它说信息已经过时了(大概5.0会有所不同) ?)。

Any help is, as always, rewarded with much appreciation and a ceremony of incense and chanting. 一如既往,任何帮助都会得到极大的赞赏,并有熏香和诵经的仪式。 Thanks!! 谢谢!!

The below creates a single table but allows for a reference/navigation property to a field you may not always wish to load (in this case, a byte array called Data ). 下面创建了一个表,但允许对您可能并不总是希望加载的字段(在这种情况下,称为Data的字节数组)进行引用/导航。 This is known as table splitting. 这称为表拆分。

Key notes are to have both entities map to the same table (duh) using TableAnnotion and share the same primary key, which is used as the foreign key for the nav properties to one another. 关键说明是TableAnnotion两个实体都使用TableAnnotion映射到同一表(duh),并共享相同的主键,该主键用作nav属性彼此之间的外键。

    [Table("Document")]
    public class Document
    {
        [Key]
        [ForeignKey("Data")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int DocumentId { get; set; }

        public string FileName { get; set; }
        public DateTime Created { get; set; }

        public virtual DocumentData Data { get; set; }
    }

    [Table("Document")]
    public class DocumentData
    {
        [Key]
        [ForeignKey("Document")]
        public int DocumentId { get; set; }

        public byte[] Data { get; set; }

        public virtual Document Document { get; set; }
    }

    public class DocEntities : DbContext
    {
        public DocEntities()
            : base("name=TestEntitiesCodeFirst")
        {
        }

        public DbSet<Document> Documents { get; set; }
    }

    static void Main(string[] args)
    {
        using (var db = new InheritTest.DocEntities())
        {
            var doc = new Document()
            {
                Created = DateTime.Now,
                FileName = "Abc.txt",
                Data = new DocumentData()
                {
                    Data = new byte[] { 0x50, 0x51, 0x52, 0x53 }
                }
            };

            db.Documents.Add(doc);

            db.SaveChanges();
        }

        using (var db = new InheritTest.DocEntities())
        {
            db.Configuration.LazyLoadingEnabled = false;

            var doc = db.Documents.First();

            if (doc.Data == null)
            {
                Console.WriteLine("doc.Data is null");
            }

            db.Entry(doc).Reference(p => p.Data).Load();

            if (doc.Data != null)
            {
                Console.WriteLine("doc.Data is not null");
                Console.WriteLine(doc.Data.Data.Length);
            }
        }

        var input = Console.ReadLine();
    }

Resulting table: 结果表:

CREATE TABLE [dbo].[Document](
    [DocumentId] [int] IDENTITY(1,1) NOT NULL,
    [FileName] [nvarchar](max) NULL,
    [Created] [datetime] NOT NULL,
    [Data] [varbinary](max) NULL,
 CONSTRAINT [PK_dbo.Document] PRIMARY KEY CLUSTERED 
(
    [DocumentId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM