我使用EF 6.1并且有两个类作为DB模型:
<html>
<head>
<style type="text/css">
p,
td,
tr,
table {
font-family: Arial;
font-size: 12px;
text-align: center;
border-left: solid 0px border-right: solid 0px border-top: solid 0px border-bottom: solid 0px border: solid 0px
}
table,
td,
tr,
th {
border-collapse: collapse;
color: #FFF;
font-family: "Arial";
font-size: 14px;
}
.master-table {
width:600px;
}
.blacktextcentre {
font-family: "Arial";
font-size: 14px;
text-align: center;
color: #000;
}
.blacktextleft {
font-family: "Arial";
text-align: left;
font-size: 14px;
color: #000;
}
.whitetextleft {
font-family: "Arial";
text-align: left;
font-size: 14px;
color: #fff;
}
.whitetextcentre {
font-family: "Arial";
text-align: center;
font-size: 16px;
color: #fff;
background-color: #A6A6A6;
}
.blacktextleftbold {
font-family: "Arial";
text-align: left;
font-size: 14px;
color: #000;
font-weight: bold;
}
.blacktextrightbold {
font-family: "Arial";
text-align: right;
font-size: 14px;
color: #000;
font-weight: bold;
}
tr.border_bottom td {
border-bottom: 1pt solid black;
}
.blacktextcentreheading {
font-family: "Arial";
font-size: 40px;
text-align: center;
color: #000;
font-weight: bold;
}
.tableborder {
background-color: #007864;
}
pre.text {
white-space: pre-line;
width: 400px;
font-family: "Arial";
text-align: left;
font-size: 14px;
color: #000;
word-wrap: break-word;
}
img {
padding: 10px;
}
</style>
</head>
<table class="master-table">
<tr class="tableborder">
<td width="20" height="20"> </td>
<td width="10" height="20"> </td>
<td height="20"> </td>
<td width="10" height="20"> </td>
<td width="20" height="20"> </td>
</tr>
<tr>
<td class="tableborder"> </td>
<td height="120"> </td>
<td>
<img src="logo" hspace="10" vspace="10">
</td>
<td> </td>
<td class="tableborder"> </td>
</tr>
<tr>
<td class="tableborder"> </td>
<td height="35"> </td>
<td class="whitetextcentre">Ticket Update</td>
<td> </td>
<td class="tableborder"> </td>
</tr>
<tr>
<td class="tableborder"> </td>
<td height="35"> </td>
<td class="blacktextleft">Dear Alex</td>
<td> </td>
<td class="tableborder"> </td>
</tr>
<tr>
<td class="tableborder"> </td>
<td height="35"> </td>
<td class="blacktextleft">The Service Desk have updated your request. Please find a summary below.</td>
<td> </td>
<td class="tableborder"> </td>
</tr>
<tr>
<td class="tableborder"> </td>
<td height="10"> </td>
<td class="blacktextleft"> </td>
<td> </td>
<td class="tableborder"> </td>
</tr>
<tr>
<td class="tableborder"> </td>
<td height="35"> </td>
<td class="whitetextcentre">Ticket Summary</td>
<td> </td>
<td class="tableborder"> </td>
</tr>
<tr>
<td class="tableborder"> </td>
<td height=""> </td>
<td>
<table width="600" border="0" cellpadding="10">
<tr class="border_bottom">
<td width="90" class="blacktextrightbold">ID:</td>
<td class="blacktextleft">IR1174</td>
</tr>
<tr class="border_bottom">
<td width="90" class="blacktextrightbold">Title:</td>
<td class="blacktextleft">Printer - Not Working</td>
</tr>
<tr class="border_bottom">
<td width="90" class="blacktextrightbold">Time Added:</td>
<td class="blacktextleft">31/05/2016 17:03:40</td>
</tr>
<tr>
<td width="90" class="blacktextrightbold">Comment:</td>
<td><pre class="text">cxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvvvvvvvv
vvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvv
vvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvv
vvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvv
vvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvvvvv
vvvvvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvv
vvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvvvvvv
vvvvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvv
vvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvv
vvvvvvvvvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvvvvvvvvvv
vvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvv
vvvvvvvvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvvvvvvvv
vvvvvvvvvvvvcxvvvvvvvvvvvvvvvvvvvvvvvvvcxvvvvvvv
vvvvvvvvvvvvvvvvvv</pre>
</td>
</tr>
</table>
</td>
<td> </td>
<td class="tableborder"> </td>
</tr>
<tr>
<td class="tableborder"> </td>
<td height="35"> </td>
<td class="whitetextcentre">Further Information</td>
<td> </td>
<td class="tableborder"> </td>
</tr>
<tr>
<td class="tableborder"> </td>
<td height="10"> </td>
<td> </td>
<td> </td>
<td class="tableborder"> </td>
</tr>
<tr>
<td class="tableborder"> </td>
<td height="35"> </td>
<td class="blacktextleft">To update your ticket then please visit the <a HREF="server">portal</a> to provide an update or click on the link to <a HREF="mailto:address?subject=[IR1174]&body=">reply</a>.</td>
<td> </td>
<td class="tableborder"> </td>
</tr>
<tr>
<td class="tableborder"> </td>
<td height="30"> </td>
<td class="blacktextcentre">Kind regards,</td>
<td> </td>
<td class="tableborder"> </td>
</tr>
<tr>
<td class="tableborder"> </td>
<td height="30"> </td>
<td class="blacktextcentre">Service Desk</td>
<td> </td>
<td class="tableborder"> </td>
</tr>
<tr class="tableborder">
<td width="20"> </td>
<td width="10"> </td>
<td> </td>
<td width="10"> </td>
<td width="20"> </td>
</tr>
</table>
</body>
</html>
如何加载具有public class A {
public int Id { get; set; }
// ...
public virtual List<B> Bs { get; set; }
}
public class B {
public int Id { get; set; }
public MyType Type { get; set; }
public byte[] Data { get; set; }
public int AId { get; set; }
[ForeignKey("AId")]
public virtual A A { get; set; }
}
enum MyType { x1, x2 }
的实体及其相关实体B.
并在断开连接模式中使用它们。
我想要这样的东西(不存在)
B.Type == x1
此查询加载所有相关实体
context.A.Include(a => a.Bs.Where(a => a.Type == x1))
我试过了:
context.A.Include(a => a.Bs)
但在关闭上下文后,访问context.Entry(a).Collection(a => a.Bs).Query()..Where(b => b.Type == x1).Load();
会引发异常。
任何建议?
修改 我也尝试在单独的查询中加载每个实体
B
但是我无法手动将var AQuery = context.A.Where ...
var aList = AQuery.ToList();
var bList = (from b in context.B
where b.Type == x1 && AQuery.Contains(b.A)
select b).ToList();
添加到b
列表中(因为我猜虚拟属性)
答案 0 :(得分:0)
我可以通过禁用延迟加载来实现我想要的功能。 (LazyLoadingEnabled = false)
我使用以下查询
var AQuery = context.A.Where ...
var aList = AQuery.ToList();
var bList = (from b in context.B
where b.Type == x1 && AQuery.Contains(b.A)
select b).ToList();
aList
项填充了适当的B,并且可以在断开连接模式下使用。