我對Action返回給我的錯誤感到困惑。我有我的經理代碼:Json的實體框架模型結果
public class AddressesManager
{
private SiteDBEntities entityContext;
public Addresses GetAddress(short id)
{
entityContext = new SiteDBEntities();
var addressList = entityContext.Addresses.Where(a => a.Id == id).FirstOrDefault();
entityContext.Dispose();
return addressList;
}
}
和行動調用這個函數:
[HttpPost]
public ActionResult LoadAddress(short id)
{
AddressesManager mngr = new AddressesManager();
Addresses address = mngr.GetAddress(id);
return new JsonResult() { Data = address };
}
和jQuery代碼至極呼叫thit行動:
$.post("/Cart/LoadAddress", { id: id })
.success(function (data) {
console.log(data);
})
.fail(function (e) { console.log(e) });
行動正在運行,但我總是得到500錯誤惠特代碼:
T他的ObjectContext實例已經處理完畢,不能再用於需要連接的操作。
據我所知,問題是與entityContext,但爲什麼會發生這種情況?我已經從執行DB數據,我也不需要連接了...
編輯:
Thit是我的地址模型。它由EF自動生成:
public partial class Addresses
{
public int Id { get; set; }
public string Title { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string Warehouse { get; set; }
public string FirstName { get; set; }
public string SecondName { get; set; }
public string Phone { get; set; }
public short DeliveryType { get; set; }
public System.Guid UserId { get; set; }
public virtual DeliveryType DeliveryType1 { get; set; }
public virtual Users Users { get; set; }
}
我查地址變量數據 - 一切OK現在和數據在這裏 – dantey89
發生這種錯誤,因爲地址類有一定的虛擬性,並且將它轉換成JSON,它要得到這些虛擬財產了。 – Kahbazi
是的,你是對的。就是這個。那麼避免這個錯誤的方法是什麼?手動映射模型? – dantey89