检索EF数据和集合的更好方法

时间:2014-01-25 16:40:12

标签: c# entity-framework linq-to-sql

所以我有一个模型,其中包含一个包含项目的模型列表,依此类推:

public partial class CART
{

    public CART()
    {
        //this.CART_DETAIL = new HashSet<CART_DETAIL>();
        this.CART_DETAIL = new List<CART_DETAIL>();
    }

    public int CART_IDE { get; set; }
    public int CART_COUNT { get; set; }
    public string SHOPPING_CART_IDE { get; set; }

    public virtual IList<CART_DETAIL> CART_DETAIL { get; set; }

}

public partial class CART_DETAIL
{
    public int CART_DETAIL_IDE { get; set; }
    public int CART_IDE { get; set; }
    public int CART_DETAIL_COUNT { get; set; }
    public Nullable<int> PACK_IDE { get; set; }
    public Nullable<int> BACKSTORE_INVENTORY_IDE { get; set; }

    public virtual CART CART { get; set; }
    public virtual PACK PACK { get; set; }
    public virtual BACKSTORE_INVENTORY BACKSTORE_INVENTORY { get; set; }
}

public partial class BACKSTORE_INVENTORY
{
    public BACKSTORE_INVENTORY()
    {
        this.CART_DETAIL = new HashSet<CART_DETAIL>();
        this.ORDER_DETAIL = new HashSet<ORDER_DETAIL>();
    }

    public int BACKSTORE_INVENTORY_IDE { get; set; }
    public int INVENT_IDE { get; set; }
    public int STORE_IDE { get; set; }
    public decimal BACKSTORE_INVENTORY_PRICE { get; set; }
    public int BACKSTORE_STOCK_QTY { get; set; }
    public decimal BACKSTORE_DISCOUNT { get; set; }
    public decimal BACKSTORE_SELLING_PRICE { get; set; }

    public virtual INVENTORY INVENTORY { get; set; }
    public virtual STORE STORE { get; set; }
    public virtual ICollection<CART_DETAIL> CART_DETAIL { get; set; }
    public virtual ICollection<ORDER_DETAIL> ORDER_DETAIL { get; set; }
}

当我打开连接并查阅数据时,一切都很好,但是如果我在视图中检索整个数据,例如,除非我将Hashset修改为List然后继续这样:

CART cart =
                    db.CART.FirstOrDefault(_item => _item.SHOPPING_CART_IDE == mShoppingCartID && _item.CART_ACTIVE_INDICATOR);

if (cart != null)
{
    cart.CART_EXP_TIME = DateTime.Now.AddMinutes(90);

    cart.USER_SESSION_IDE = UserSessionManager.GetUserSession().mUserSessionID;

    cart.CART_DETAIL = cart.CART_DETAIL.ToList();

    foreach (var cartDetail in cart.CART_DETAIL)
    {
        if(cartDetail.BACKSTORE_INVENTORY_IDE != null)
        {
            cartDetail.BACKSTORE_INVENTORY =
                db.BACKSTORE_INVENTORY.First(_item => _item.BACKSTORE_INVENTORY_IDE == cartDetail.BACKSTORE_INVENTORY_IDE);

            cartDetail.BACKSTORE_INVENTORY.INVENTORY =
                db.INVENTORY.Find(cartDetail.BACKSTORE_INVENTORY.INVENT_IDE);

            cartDetail.BACKSTORE_INVENTORY.INVENTORY.CARD =
                db.CARD.Find(cartDetail.BACKSTORE_INVENTORY.INVENTORY.CARD_IDE);
        }
        else
        {
            cartDetail.PACK = db.PACK.First(_item => _item.PACK_IDE == cartDetail.PACK_IDE);
        }
    }

    db.SaveChanges();
}

我收到以下错误:CS0021: Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.ICollection<MyApp.Models.DAL.Entities.CART_DETAIL>'我理解是因为ICollection不能提供索引,然后我得到The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.我忘了回溯的项目。

所以我的问题:是什么让这种情况发生的?有没有办法一次检索所有数据而无需单独获取所有特定项目?做事的更好方法是什么?

1 个答案:

答案 0 :(得分:1)

您想通过上述代码实现什么目标?

我正在努力遵循你的最终目标,但是这些方面的东西会成为你想要的东西:

  public List<Cart> GetAllInCart()
  {
     return db.CART.Where(a => a.Cart_IDE == CartIDE)
                   .Include(x => x.Cart_Detail)
                   .Include(x => x.Cart_Detail.Pack)
                   .Include(x => x.Cart_Detail.Backstore_Inventory)
                   .ToList()
  }

我希望这会有所帮助:)