AspNet Core WebApi OData-多对多联接表 [英] AspNet Core WebApi OData - Many to Many join tables

查看:80
本文介绍了AspNet Core WebApi OData-多对多联接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这几天,当您进行多对多连接时,这是EF Core中的推荐模式,我们将执行以下操作:

As is the recommended pattern in EF Core these days when you have a many to many join we do something like this:

Fluent API,实体框架中的多对多核心

我已经完成了有关如何在OData模型中公开它的问题.

Having done that I'm faced with the issue as to how I go about exposing that in the OData model.

从技术上讲,实体类型定义没有键属性(因为它使用复合键,所以OData框架不喜欢我将集合添加到模型中.

Since technically the Entity type definition has no key property (as it uses a composite key the OData framework doesn't like me adding the set to the model.

对于此问题,推荐的方法是什么?

Whats the recommended approach to this problem?

推荐答案

似乎EF和OData已经有点同步了,如果他们可以逐字地共享模型构建代码,那就更好了.

It seems that EF and OData have become somewhat synced up, what would be even better would be if they could literally share model building code.

为此,我发现在调用AddSet之后在OData模型构建中,我能够以与在EF中相同的方式来定义键...

To that end I found that in the OData model build after calling AddSet I was able to define the key in the same way as I did in EF ...

Builder.EntityType<UserRole>().HasKey(ac => new { ac.UserId, ac.RoleId });

这有点干净,我还没有尝试过发布或直接请求这种类型,但是从关系链的任何一侧扩展似乎都可以.

This is somewhat clean, I have not yet tried posting or directly requesting such a type yet, but expanding from either side of the relationship chain seems to work fine.

在控制器中对此的详细说明...

Moredetails on the definition of this in controller ...

网址需要包含两个关键部分...HTTP GET〜/UserRole(123,456)

The Url needs to contain both key parts ... HTTP GET ~/UserRole(123, 456)

与PUT和DELETE相同,但POST不包含它们是主体对象一部分的键.

the same with PUTs and DELETEs but POST's don't contain the key they are part of the object in the body.

方法签名必须包含两个关键部分,这是一些示例...

The method signature must contain both key parts, here's some examples ...

public IActionResult Get([FromODataUri]int keyUserId, [FromODataUri]Guid keyRoleId)
{
    ...
}

public IActionResult Put([FromODataUri]int keyUserId, [FromODataUri]Guid keyRoleId)
{
    ...
}

public IActionResult Post(UserRole entity)
{
    ...
}

这篇关于AspNet Core WebApi OData-多对多联接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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