简体   繁体   English

在.NET Core Web API中为MongoDB使用OData

[英]Using OData in .NET Core Web API for MongoDB

OData is now supported in .NET Core and 7.2.0 was released. 现在,.NET Core支持OData,7.2.0已发布。 But can it be used with MongoDB? 但它可以与MongoDB一起使用吗? I have searched, but I could not find anything that says one way or the other. 我已经搜索过,但是找不到任何说法的东西。

EDIT: 编辑:

I've found a nuget package https://www.nuget.org/packages/microsoft.aspnetcore.odata and in ConfigureServices I've added this: 我找到了一个nuget包https://www.nuget.org/packages/microsoft.aspnetcore.odata ,在ConfigureServices我添加了这个:

And this seems to work for me: 这似乎对我有用:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddOData();
    services.AddSingleton<IODataModelManger, ODataModelManager>(DefineEdmModel);
    ...
}

private ODataModelManager DefineEdmModel(IServiceProvider services)
{
    var modelManager = new ODataModelManager();

    var builder = new ODataConventionModelBuilder();
    builder.EntitySet<TestDTO>(nameof(TestDTO));
    builder.EntityType<TestDTO>().HasKey(ai => ai.Id); // the call to HasKey is mandatory
    modelManager.AddModel(nameof(Something), builder.GetEdmModel());

    return modelManager;
}

Controller 调节器

[HttpGet("all")]
public async Task<IQueryable<TestDTO>> Get()
{
    // plug your entities source (database or whatever)
    var test = await TestService.GetTest();

    var modelManager = (IODataModelManger)HttpContext.RequestServices.GetService(typeof(IODataModelManger));
    var model = modelManager.GetModel(nameof(Something));
    var queryContext = new ODataQueryContext(model, typeof(TestDTO), null);
    var queryOptions = new ODataQueryOptions(queryContext, HttpContext.Request, Provider);

    return queryOptions
        .ApplyTo(test, new ODataQuerySettings
        {
            HandleNullPropagation = HandleNullPropagationOption.True
        }, null)
        .Cast<TestDTO>();
}

Service 服务

public async Task<IQueryable<TestDTO>> GetTest()
{
    return await GenericRepository.TestAll();
}

Repositories

public async Task<IQueryable<TEntity>> TestAll()
{
    var res = new GetManyResult<TEntity>();
    try
    {
        DateTime startTime = DateTime.Now;
        var collection = GetCollection<TEntity>().AsQueryable();
        var entities = collection.ToArray<TEntity>().AsQueryable();
        return entities
}

But is this the best way to do it? 但这是最好的方法吗?

I mean, shouldn't the collection contain only the elements that meet the filters, beeing more optimised? 我的意思是,该集合是否应仅包含符合过滤器的元素,更优化?

If yes, how do I achieve this? 如果是,我该如何实现?

I think theres only currently one connected service available in the visual studio market place for MongoDB. 我认为目前只有MongoDB视觉工作室市场提供的连接服务。 Link Here. 链接在这里。

ODBC Driver for MongoDB provides high-performance and feature-rich connectivity solution for ODBC-based applications to access MongoDB databases from Windows, MacOS, Linux. 用于MongoDB的ODBC驱动程序为基于ODBC的应用程序提供了高性能和功能丰富的连接解决方​​案,以便从Windows,MacOS,Linux访问MongoDB数据库。 Full support for standard ODBC API functions, MongoDB data types and SQL queries implemented in our driver makes interaction of your database applications with MongoDB fast, easy and extremely handy. 完全支持标准ODBC API函数,MongoDB数据类型和在我们的驱动程序中实现的SQL查询使您的数据库应用程序与MongoDB的交互快速,简单且非常方便。

Looks like it would handle all of the things you'd expect it to when connecting to MongoDB. 看起来它可以处理连接到MongoDB时所期望的所有事情。

However it's worth noting that, that is only a trail and I've been unable to find any 'open source' versions 然而值得注意的是,这只是一条小道,我一直无法找到任何“开源”版本

MongoDB OData Connector http://cdn.cdata.com/help/DGB/cd/ Its not free https://www.cdata.com/drivers/mongodb/download/ Overview MongoDB OData Connector http://cdn.cdata.com/help/DGB/cd/它不是免费的https://www.cdata.com/drivers/mongodb/download/概述

The MongoDB OData Connector application enables you to securely access data from MongoDB in popular formats like OData, JSONP, SOAP, RSS, and more. MongoDB OData Connector应用程序使您能够以流行的格式安全地访问MongoDB中的数据,如OData,JSONP,SOAP,RSS等。 The Getting Started section explains how to establish the connection to MongoDB. “入门”部分介绍了如何建立与MongoDB的连接。 In this section, you will find a guide to setting required connection properties and allowing the OData connector to access MongoDB tables. 在本节中,您将找到设置所需连接属性并允许OData连接器访问MongoDB表的指南。 The Supported OData section shows the OData syntax supported by the OData connector and points out any limitations when querying live data. 支持的OData部分显示OData连接器支持的OData语法,并指出查询实时数据时的任何限制。 The OData connector can be installed as a stand-alone application or integrated with your server. OData连接器可以作为独立应用程序安装,也可以与服务器集成。 In the Server Configuration section you will find information on how to install the OData connector on an existing server configuration. 在“服务器配置”部分中,您将找到有关如何在现有服务器配置上安装OData连接器的信息。 System requirements are also listed here. 此处还列出了系统要求。 You will also find instructions on how to manage users and deploy SSL. 您还可以找到有关如何管理用户和部署SSL的说明。 Logging details the available logging resources. 记录详细信息可用的日志记录资源。 The OData API enables access to your data from any application with Web connectivity. OData API支持从任何具有Web连接的应用程序访问您的数据。 The OData connector supports all major authentication schemes. OData连接器支持所有主要的身份验证方案。 This section documents HTTP methods supported by the server, server responses, and supported authentication schemes. 本节介绍服务器支持的HTTP方法,服务器响应和支持的身份验证方案。 The Data Model section lists the tables, views, and stored procedures available for the application. “数据模型”部分列出了可用于应用程序的表,视图和存储过程。

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

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