我在 VS 2010 中使用 EF 3.5。我有一个返回结构的方法。在结构中有一个对象 armatuur。当结构返回时,我想从 armatuur 实例访问相关对象。
然而
返回结构的方法:
public LampPostDetail getLamppostInfo(int id)
{
LampPostDetail lpd;
lpd.xPos = 0;
lpd.ypos = 0;
lpd.armatuur = new Armatuur();
//get the info from object
using (var db = new OvisionDBEntities())
{
var objects = from o in db.Objects
where o.ObjectId == id
select o;
foreach (OVSL.Data.Object o in objects)
{
lpd.xPos = o.XCoordinatie;
lpd.ypos = o.YCoordinatie;
lpd.armatuur = o.Armatuur; //which is a table in my db
}
return lpd;
}
}
结构:
public struct LampPostDetail
{
#region [ Data Members (14)]
//lamppost info
public double? xPos;
public double? ypos;
//a lamppost can have several armaturen
public OVSL.Data.Armatuur armatuur; //is a table in my db
#endregion [ Data Members ]
}
在我的客户中执行此操作时:
LampPostDetail lpd = client.getLamppostInfo(id);
string brand = lpd.armatuur.producer.name; //producer is related object of armatuur
我得到一个 ObjectDisposedException。我知道发生这种情况是因为 LampPostDetail 对象在 using 块完成后被释放。但是我怎样才能让它工作呢?在我将其返回给客户之前检索我需要的所有信息(例如品牌名称)不是一种选择。