简体   繁体   English

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

[英]A better way to retrieve EF data and collections

So I have a model that contains a list of models which contains items, and so on, like this: 因此,我有一个包含包含项目的模型列表的模型,依此类推:

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; }
}

When I open a connection and consult the data, everything's fine, but if I retrive the whole data in a view, for example, unless I modify the Hashset to a List and then proceed like this: 当我打开一个连接并查阅数据时,一切都很好,但是,例如,如果我在视图中检索了整个数据,除非将Hashset List修改为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();
}

I get the following error: CS0021: Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.ICollection<MyApp.Models.DAL.Entities.CART_DETAIL>' which I understand is because the ICollection does not afford indexing, and then I get The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. 我收到以下错误: 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. for items that I forgot to retrive. 我忘记找回的物品。

So my question: what makes this happen? 所以我的问题是:什么使这种情况发生? Is there a way to retrieve all the data at once without having to get all specific items separately? 有没有一种方法可以立即检索所有数据而不必分别获取所有特定项目? A better way to do things? 做事更好的方法?

What are you trying to achieve form the above code? 您想通过上述代码实现什么?

I am struggling to follow what your end goal is but would something along these lines be what you are looking for: 我正在努力遵循您的最终目标,但是您正在寻找符合以下要求的东西吗?

  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()
  }

I hope this helps :) 我希望这有帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 EF Core 中更新数据的更好方法是什么 - What is better way to update data in EF Core 有什么更好的方法来实现静态全局集合? - What is better way to implement static global collections? ef7无法检索子集合的子对象的属性 - ef7 unable to retrieve child collections' child object's properties 通过EF更新基础查找的更好方法? - Better way to update the underlying lookup via EF? 使用 EF 更新/插入值的更好方法 - Better way to update/insert values using EF EF模型的动态字段〜必须有更好的方法 - dynamic fields for EF model ~ there has to be a better way 有没有更好的方法可以在EF中实现OnModelCreating方法? - Is there a better way to implement OnModelCreating method in EF? Linux上的Dotnet核心1.1:在使用EF Core播种数据时,有什么特别的方式比其他方式更好? - Dotnet core 1.1 on linux: Any particular way thats better than others when seeding data with EF Core? 在 ASP.NET Core 中使用 EF Core 加载单个实体及其相关数据的更好方法是什么? - Which is the better way to load a single entity with its related data with EF Core in ASP.NET Core? EF或SQL是审核数据更改的更好选择吗? - Is EF or SQL the better choice to audit data changes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM