实体框架中的DbSet [英] DbSet in Entity Framework

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

问题描述

我在实体框架中有以下代码:

I have the following code in Entity Framework:

using(var dbc = new TestDbContext())
{
    var data = (from a in dbc.tableList
                select new { a = id }).ToList();
}

在调试代码时,我遇到了下面的代码

When I was debugging the code I came across the following piece of code

public class TestDbContext : DbContext
{
     public TestDbContext()
     { 
     }

     public DbSet<Table> tableList {get;set;}
}

我想知道甚至不创建实例的 DbSet< Table> 像这样:

I am wondering like without even creating an instance of DbSet<Table> like this :

public Dbset<Table> tableList = new Dbset<Table>();

我如何查询表,例如:

in

var data = (from a in dbc.tableList
            select new { a = id }).ToList();


推荐答案

这没有魔力。 TestDbContext 源自 DbContext

DbContext 调用内部类 DbSetDiscoveryService ,该类使用反射来迭代 DbSet 属性并对其进行初始化。

DbContext makes a call to internal class DbSetDiscoveryService which uses Reflection to iterate through DbSet properties and initialize them.

EF的单声道版本,您可以在 Github ,其中显示了对 InitializeSets

The mono version of EF, you can find the code on Github that shows the call to InitializeSets

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

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