linqToSql相关表未正确延迟加载。根本没有人口

linqToSql相关表未正确延迟加载。根本没有人口,linq,linq-to-sql,Linq,Linq To Sql,我有两个表,它们的关系结构与标准订单、订单行表类似。 创建数据上下文时,它为Order类提供了OrderLines属性,该属性应填充该特定Order对象的OrderLine对象。 当然,默认情况下,它会延迟加载OrderLine属性中的内容,但这应该是相当透明的,对吗 好的,这里是我的问题:当我转到MyOrder.OrderLines时,我得到了一个空列表,但是当我转到myDataContext.OrderLines.Where(line=>line.OrderId==1)时,我得到了正确的列表

我有两个表,它们的关系结构与标准订单、订单行表类似。
创建数据上下文时,它为Order类提供了OrderLines属性,该属性应填充该特定Order对象的OrderLine对象。
当然,默认情况下,它会延迟加载OrderLine属性中的内容,但这应该是相当透明的,对吗

好的,这里是我的问题:当我转到
MyOrder.OrderLines
时,我得到了一个空列表,但是当我转到
myDataContext.OrderLines.Where(line=>line.OrderId==1)
时,我得到了正确的列表

public void B()
{
  var dbContext = new Adis.CA.Repository.Database.CaDataContext(
    "<connectionString>");
  dbContext.Connection.Open();
  dbContext.Transaction = dbContext.Connection.BeginTransaction();
  try
  {
    //!!!Edit: Imortant to note that the order with orderID=1 already exists 
    //!!!in the database

    //just add some new order lines to make sure there are some
    var NewOrderLines = new List<OrderLines>()
    {
      new OrderLine() { OrderID=1, LineID=300 },
      new OrderLine() { OrderID=1, LineID=301 },
      new OrderLine() { OrderID=1, LineID=302 },
      new OrderLine() { OrderID=1, LineID=303 }
    };

    dbContext.OrderLines.InsertAllOnSubmit(NewOrderLines);
    dbContext.SubmitChanges();

    //this will give me the 4 rows I just inserted
    var orderLinesDirect = dbContext.OrderLines
          .Where(orderLine => orderLine.OrderID == 1);

    var order = dbContext.Orders.Where(order => order.OrderID == 1);
    //this will be an empty list
    var orderLinesThroughOrder = order.OrderLines;
  }
  catch (System.Data.SqlClient.SqlException e)
  {
    dbContext.Transaction.Rollback();
    throw;
  }
  finally
  {
    dbContext.Transaction.Rollback();
    dbContext.Dispose();
    dbContext = null;
  }
}
public void B()
{
var dbContext=new Adis.CA.Repository.Database.CaDataContext(
"");
dbContext.Connection.Open();
dbContext.Transaction=dbContext.Connection.BeginTransaction();
尝试
{
//!!!Edit:i重要的是要注意orderID=1的订单已经存在
//!!!在数据库中
//只需添加一些新的订单行,以确保有一些
var NewOrderLines=新列表()
{
new OrderLine(){OrderID=1,LineID=300},
new OrderLine(){OrderID=1,LineID=301},
new OrderLine(){OrderID=1,LineID=302},
new OrderLine(){OrderID=1,LineID=303}
};
dbContext.OrderLines.InsertAllOnSubmit(NewOrderLines);
dbContext.SubmitChanges();
//这将为我提供刚才插入的4行
var orderLinesDirect=dbContext.OrderLines
.Where(orderLine=>orderLine.OrderID==1);
var order=dbContext.Orders.Where(order=>order.OrderID==1);
//这将是一个空列表
var orderLinesThroughOrder=order.OrderLines;
}
catch(System.Data.SqlClient.SqlException)
{
dbContext.Transaction.Rollback();
投掷;
}
最后
{
dbContext.Transaction.Rollback();
dbContext.Dispose();
dbContext=null;
}
}
据我所知,我没有做任何特别奇怪的事情,但我认为
orderLinesDirect
orderLinesThroughOrder
会给我相同的结果集。

有人能告诉我为什么没有吗?

您只是在添加订单行;没有任何实际订单。因此,dbContext.Orders上的Where返回一个空列表

你怎么还能在订单上找到物业订单行我不明白,所以我可能在这里搞砸了

[编辑]
您能否更新示例以显示实际类型,尤其是order变量的类型?在我看来,它应该是一个
IQueryable
,但奇怪的是,你可以
。订单行
。尝试在Where之后添加
First()
FirstOrDefault()

Ah yes。我的例子有一个问题。我没有指出OrderID=1的订单已经存在