简体   繁体   English

C# 实体框架连接表

[英]C# Entity Framework join tables

I am trying to get the same results as with a SQL query using Entity Framework method syntax.我试图获得与使用实体框架方法语法的 SQL 查询相同的结果。

在此处输入图片说明

SQL query : SQL 查询:

select 
    mr.*, mrf.userId as RequesterUserId, mrt.UserId as ReceiverUserId 
from 
    MoneyRequests mr
inner join  
    MoneyReqFrom mrf on mr.MoneyRequestId = mrf.MoneyRequestId
inner join  
    MoneyReqTo mrt on mr.MoneyRequestId = mrt.MoneyRequestId
where 
    mr.MoneyRequestId = 'acfc8008-4cf7-47ec-a3fe-0fe245af77cc'

EF Linq method syntax : EF Linq 方法语法:

var moneyreqResponse = context.MoneyRequests
                              .Join(context.MoneyReqFroms, 
                                      mr => mr.MoneyRequestId, 
                                      mrf => mrf.MoneyRequestId, 
                                      (mr, mrf) => new 
                                                   { 
                                                       MoneyRequestId = mr.MoneyRequestId,
                                                       Amount = mr.Amount,
                                                       RequestType = mr.RequestType,
                                                       CreationDate = mr.CreationDate,
                                                       RequesterUserId = mrf.UserId 
                                                    }) 
                              .Join(context.MoneyReqTos, 
                                       mr => mr.MoneyRequestId, 
                                       mrt => mrt.MoneyRequestId,
                                       (mr, mrt) => new 
                                                    { 
                                                        MoneyRequestId = mr.MoneyRequestId,
                                                        Amount = mr.Amount, 
                                                        RequestType = mr.RequestType,
                                                        CreationDate = mr.CreationDate,
                                                        ReceiverUserId = mrt.UserId, 
                                                        Email = mrt.Email 
                                                    }) 
                              .Where(fullEntry => fullEntry.MoneyRequestId == "acfc8008-4cf7-47ec-a3fe-0fe245af77cc") 
                              .ToList();

I retrieve the data from the database except the column RequesterUserId .我从数据库中检索数据,除了RequesterUserId列。

Do you know why?你知道为什么吗?

Thanks谢谢

Your query returns a MoneyRequests type, which I belive does not contain RequesterUserId .您的查询返回MoneyRequests类型,我相信它不包含RequesterUserId

You should define a new type with all the properties you need (those returned by the join) and add it to your DbContext.您应该使用您需要的所有属性(由连接返回的属性)定义一个新类型,并将其添加到您的 DbContext。

Also probably you want to mark your new type as keyless也可能您想将新类型标记为无钥匙

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

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