Ger*_*hes 11 c# mongodb odata asp.net-web-api asp.net-core
现在,.NET Core支持OData,7.2.0已发布.但它可以与MongoDB一起使用吗?我已经搜索过,但是找不到任何说法的东西.
编辑:
我找到了一个nuget包https://www.nuget.org/packages/microsoft.aspnetcore.odata,ConfigureServices
我在这里添加了这个:
这似乎对我有用:
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;
}
Run Code Online (Sandbox Code Playgroud)
调节器
[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>();
}
Run Code Online (Sandbox Code Playgroud)
服务
public async Task<IQueryable<TestDTO>> GetTest()
{
return await GenericRepository.TestAll();
}
Run Code Online (Sandbox Code Playgroud)
库
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
}
Run Code Online (Sandbox Code Playgroud)
但这是最好的方法吗?
我的意思是,该集合是否应仅包含符合过滤器的元素,更优化?
如果是,我该如何实现?
我认为目前 MongoDB 的 Visual Studio 市场中只有一项可用的连接服务。链接在这里。
MongoDB 的 ODBC 驱动程序为基于 ODBC 的应用程序提供高性能且功能丰富的连接解决方案,以从 Windows、MacOS、Linux 访问 MongoDB 数据库。我们的驱动程序中实现了对标准 ODBC API 函数、MongoDB 数据类型和 SQL 查询的全面支持,使您的数据库应用程序与 MongoDB 的交互变得快速、简单且极其方便。
看起来它可以处理连接到 MongoDB 时您期望的所有事情。
但值得注意的是,这只是一条线索,我无法找到任何“开源”版本
归档时间: |
|
查看次数: |
3689 次 |
最近记录: |