如何通过名称从上下文中选择特定的DbSet [英] How to choose specific DbSet from context by name

查看:133
本文介绍了如何通过名称从上下文中选择特定的DbSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆具有相同结构的DbSet,所以我可以将它们全部称为MyObject.在我的EntityFramework中,我有很多看起来相同的实体,但还有其他含义.所有实体类型都有6个列/字段(相同).

I have a bunch of DbSets that have the same structure so I can call them all as MyObject. In my EntityFramework I have a lot of entities that look the same, but have some other meaning. All entity types have 6 column/fields (the same).

现在,如何仅基于DbSet名称从上下文中选择合适的集合?

Now, how can I choose the proper set from context basing only on DbSet name?

context.Set(Type type)

不好,因为只要需要特定的DbSet<OneOfEntities>,我就不能用它调用LINQ for Entities.

is not OK, because I can't call LINQ for Entities with it as long as I need a specific DbSet<OneOfEntities>.

缺少什么?

更新:

我想出了通过反射获得实体信息的想法,但这似乎也是一个死胡同,因为我仍然无法从代码中确定每个实体的特定数据类型.即使我有表名,例如MyObject和另一个MyObject2MyObject3等.我无法将其转换为EDMX模型中定义的类.我尝试将Type.GetType()与程序集混合使用,或通过将类名(表名)连接到一个字符串中的名称空间来进行尝试.还是一无所有.

I've come up with idea of getting entities info by using reflecion but it seems to be a dead end as well, because still I can't determine from code the specific datatype for each of entities. Even when I have the table name, ex. MyObject and another MyObject2, MyObject3, etc. I can't transform it to the class which is defined in EDMX model. I tried something with Type.GetType() mixed with assemblies or by concatenating class name (table name) to namespace in one string. Still nothing.

推荐答案

使用通用存储库方法.检出此示例: https://docs.microsoft.com/zh-cn/aspnet/mvc/概述/旧版本/使用ef-5-using-mvc-4入门/在ASP.NET MVC中实现存储库和工作单元模式-应用程序

Use a generic repository approach. Check this example out: https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

基本上,您有类似这样的内容:

Basically, you have something like this:

public class GenericRepository<TEntity> where TEntity : class
{
    internal SchoolContext context;
    internal DbSet<TEntity> dbSet;

    public GenericRepository(SchoolContext context)
    {
        this.context = context;
        this.dbSet = context.Set<TEntity>();
    }
    //Other code
}

我已经使用这种方法很多年了,而且效果很棒!!这样,您就不必与EF捆绑在一起了.记住,您必须包装您的类,以免卡在框架上. .NET Core EF有很大的不同,如果没有包装,升级将非常困难.

I have used this approach for years, and it works awesome!! That way you are not tied to EF. Remember, you have to wrap your classes so you don't get stuck on a framework. The .NET Core EF is very different, and the upgrade would be very hard without a wrapper.

我注意到您对具有相同属性的实体的评论.我将使用这样的抽象类:

I noticed your comment about the entities having the same properties. I would use an abstract class like this:

public abstract class EntityBase
{
     //Add properties
}

然后让您的实体从此类继承:

Then make your entity inherit from this class:

public class MyExampleClass : EntityBase
{
     //Add other properties
}

最后,将GenericRepository更改为TEntity,使其成为EntityBase的类型:

Finally, change the GenericRepository to for TEntity to be a type of EntityBase:

public class GenericRepository<TEntity> where TEntity : EntityBase
{
    //Reference code from example
}

我以前使用过它,效果很好!

I have used this before and it works great!

〜干杯

这篇关于如何通过名称从上下文中选择特定的DbSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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