简体   繁体   English

是否可以在实体框架中返回实体的所有列数据

[英]Is it possible to return all columns data of an entity in Entity Framework

Is it possible for a entity class 实体类是否可能

class mytest
{
    public int id {get;set;}
}

with Entity Framework to query this entity and get all its column out without them being present in the C# class? 使用Entity Framework查询该实体并获取其所有列,而C#类中不存在它们?

Given a table I want to filter based on some columns I know is present and then return all the other columns still, in the case above I know the table as a id column. 给定一个表,我想根据已知的某些列进行过滤,然后仍然返回所有其他列,在上述情况下,我将该表称为id列。

You will have to have all the properties that you want materialised into your .Net code defined in the entity class, but you set an interface on the class to defined the "known" fields that you want to query on, such as ID: 您必须在实体类中定义要包含在.Net代码中的所有属性,但是您需要在该类上设置一个接口以定义要查询的“已知”字段,例如ID:

public interface IEntity
{
    int Id { get; set; }
}

And then you can use EF to query those defined fields, or you can use the generic methods on the EF context to query certain fields that every entity must have, such as Id: 然后,您可以使用EF查询那些已定义的字段,也可以在EF上下文中使用通用方法查询每个实体必须具有的某些字段,例如Id:

context.Set<TEntity>().Find(id);

or 要么

context.Set<TEntity>().Where(predicate);

The above assumes TEntity : class, IEntity 上面假设TEntity : class, IEntity

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

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