简体   繁体   English

如何使用EntityFramework动态指定实体以检索数据?

[英]How to dynamically specify entity to retrieve data using EntityFramework?

I have a list of entity lookups which are called Standards. 我有一个称为标准的实体查找列表。 Eg Countries, ProductTypes, PortofEntry, Pests etc... The purpose of these entities is to be consumed by third party providers and the goal is to create one Web API endpoint to be consume in which a provider will specify the Standard they require and the response will be a JSON list. 例如国家/地区,产品类型,入口,病虫害等...这些实体的目的是由第三方提供商使用,并且目标是创建一个要使用的Web API端点,提供商将在其中指定所需的标准以及响应将是一个JSON列表。

For the backend I am using EntityFramework 6, and that is where I am a bit stuck as I am trying to get the data and it takes long to execute. 对于后端,我使用的是EntityFramework 6,在尝试获取数据的过程中,这有点卡住了,执行起来需要很长时间。

public Response<IEnumerable<dynamic>> GetStandardsByName(string name)
{
            Type standardType = typeof(eCertModel).Assembly.DefinedTypes.SingleOrDefault(p=> p.Name == name);

            if (standardType == null)
            {
                return new Response<IEnumerable<object>>
                {
                    IsSuccessful = false,
                    Message = "Not a valid standard",
                    Data = null
                };
            }

            **dynamic standardData = _db.Set(standardType).ToListAsync().Result;**

            return new Response<IEnumerable<dynamic>>
            {
                Data = standardData,
                IsSuccessful = true,
                Message = $"Standard '{name}' returned successfully!"
            };
        }

Is there a better way I can do this? 有没有更好的方法可以做到这一点? The reason why I do not want to have different endpoints, that would me I have to cater for all 52 standards, which is messy to maintain. 我不想拥有不同的端点的原因是我必须迎合所有52个标准,而这些标准很难维护。 And it seems it could be the call to ToListAsync which is the only one availabe. 看来这可能是对ToListAsync的调用,这是唯一可用的方法。 And yes, I started off using async-await but this was more troublesome, from the controller to this method, the whole app froze... 是的,我开始使用async-await,但这从控制器到这种方法都比较麻烦,整个应用程序都冻结了……

There's no need to mess around with Dynamic. 无需弄乱Dynamic。 Just change your controller's return type to HttpResponseMessage and serialize the response data and send it to the client yourself. 只需将控制器的返回类型更改为HttpResponseMessage并序列化响应数据,然后将其发送给客户端即可。 You're opting out of the helpful message formatting by having a controller that returns many different shapes of data, so there's no reason not to specify thh HTTP response message directly. 您通过让控制器返回许多不同形状的数据来选择有用的消息格式,因此没有理由不直接指定HTTP响应消息。

Of course "The reason why I do not want to have different endpoints, that would me I have to cater for all 52 standards, which is messy to maintain" isn't really a good reason. 当然,“我不想拥有不同端点的原因,那就是我必须迎合所有52条标准,这很难维持”,这并不是一个很好的理由。 You could easilly auto-generate the controllers. 您可以轻松地自动生成控制器。

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

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