用户登录
用户注册

分享至

创建一个 DbSet<T>在实体框架中动态?

  • 作者: z35235367
  • 来源: 51数据库
  • 2022-12-13

问题描述

在 LINQ to SQL 中,我可以使用 DataContext.GetTable 动态创建存储库.除了在特定的 DbContext 上声明属性之外,是否有类似的方法可以在实体框架 4 中执行此操作?例如:

In LINQ to SQL, I can create a repository dynamically using DataContext.GetTable<T>. Is there a similar way to do this in Entity Framework 4 other than declaring the properties on the specific DbContext? For example:

public MyDbContext: DbContext
{
    public DbSet<MySet> MySets {get; set;}
}

我想知道如何像使用 LINQ to SQL 那样动态创建/获取对 MySets 的引用:

I would like to know how can I create/get a reference to MySets dynamically as I can with LINQ to SQL as in:

var mySet = MyDbContext.GetTable<MySet>();

推荐答案

DbContext 对此有方法:

  var set = context.Set<MyEntity>();
软件
前端设计
程序设计
Java相关