EF Core中的条件导航属性

时间:2019-10-27 20:43:54

标签: c# sql-server .net-core entity-framework-core

我有一个User表,其中包含一个跟踪更改的相应UserAudits表。操作类型存储在审核表中(插入,更新,删除)。

我想在用户实体上创建一个导航属性,以便可以从审核表中获取“插入”记录。这可能吗?通常,在设置导航属性时,无法在联接上指定额外条件。

用户:

public class User
{
    public int Id {get; set;}
    public string Name {get;set;}
    public UserAudit InsertedAuditRecord {get; set;} // <-- How to configure this?
}

UserAudit:

public class UserAudit
{
    public int AuditId {get; set;
    public int Id {get; set;}
    public string Name {get; set;}
    public string OperationType {get; set;}
}

我知道我可以在User实体上创建一对多导航属性,然后查询:

ICollection<UserAudit> UserAudits {get; set;}
UserAudit InsertedAuditRecord => UserAudits.First(ua => ua.OperationType = "insert");

但这将需要将整个审核表供用户查找,以查找单个记录。

肯定有某种方法可以做到这一点?

2 个答案:

答案 0 :(得分:1)

我猜您要查找的内容(其中使用 <?php namespace App\Http\Controllers; .. use Omnipay\Omnipay; class PurchaseController extends Controller { public $gateway; public function __construct() { $this->gateway = Omnipay::create('PayPal_Rest'); $this->gateway->setClientId(...); $this->gateway->setSecret(...); $this->gateway->setTestMode(true); } protected function makePurchase(Request $request) { try { $items = ...; $response = $this->gateway->purchase(array( 'amount' => $cart->getSum(), 'items' => $items, 'currency' => config('paypal.currency'), 'returnUrl' => route('payment.success'), 'cancelUrl' => route('cart'), ))->send(); if ($response->isRedirect()) { $response->redirect(); // this will automatically forward the customer } else { // not successful return $response->getMessage() . ", " . $response->getCode(); } } catch(Exception $e) { return $e->getMessage(); } } protected function purchaseCompleted(Request $request) { ... } } 子句的 Include())现在已添加到 Where()。看看this答案。

答案 1 :(得分:-1)

子记录上的WhereFirst)不会带回整个子对象。