简体   繁体   English

实体框架不加载相关对象

[英]Entity Framework do not load related objects

I'm developing the Music Store sample app with ASP.NET MVC, Entity Framework and WCF. 我正在使用ASP.NET MVC,实体框架和WCF开发音乐商店示例应用程序。

This is a layered application which has a common layer for the entities. 这是一个分层的应用程序,具有用于实体的公共层。

in the AddToCart Action method , the Album object is populates fine but after the Cart save when wcf loads the cart object the associated Album object is Null, may be some serialization issue (I have not idea), in the view @foreach (var item in Model.CartItems) the item.Album.Title becomes null AddToCart Action方法中, Album对象填充良好,但是在wcf加载购物车对象时购物车保存后,关联的Album对象为Null,可能是一些序列化问题(我不知道),在视图@foreach (var item in Model.CartItems)item.Album.Title变为null

This is my code: 这是我的代码:

public static void AddToCart(Album album, string ShoppingCartID)
{
            using (MusicStoreEntities db = new MusicStoreEntities())
            {
                // Get the matching cart and album instances
                var cartItem = db.Carts.SingleOrDefault(
                    c => c.CartId == ShoppingCartID
                         && c.AlbumId == album.AlbumId);

                if (cartItem == null)
                {
                    // Create a new cart item if no cart item exists
                    cartItem = new Cart
                    {
                        AlbumId = album.AlbumId,
                        CartId = ShoppingCartID,
                        Count = 1,
                        DateCreated = DateTime.Now                           
                    };

                    db.Carts.Add(cartItem);
                }
                else
                {
                    // If the item does exist in the cart, then add one to the quantity
                    cartItem.Count++;
                }

                // Save changes
                db.SaveChanges();
            }
}

public static List<Cart> GetCartItems(string ShoppingCartID)
        {
            using (MusicStoreEntities db = new MusicStoreEntities())
            {
               return db.Carts.Where(cart => cart.CartId == ShoppingCartID).ToList();

            }
        }

Controller 调节器

namespace MusicStore.Web.Controllers
{
    public class ShoppingCartController : Controller
    {
        MusicShoppingCartMgr.Cart serviceref1 = new MusicShoppingCartMgr.Cart();
        MusicShoppingCartMgr.iShoppingCart servicemethodref1 = new iShoppingCartClient();
        //
        // GET: /ShoppingCart/
        public ActionResult Index()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);

            // Set up our ViewModel
            var viewModel = new ShoppingCartViewModel
            {
                CartItems = cart.GetCartItems(cart.ShoppingCartId),
                CartTotal = cart.GetTotal(cart.ShoppingCartId)
            };

            // Return the view
            return View(viewModel);
        }
        //
        // GET: /Store/AddToCart/5

        public ActionResult AddToCart(int id)
        {
            var addedAlbum = servicemethodref1.GetAlbum(id);

            // Add it to the shopping cart
            var cart = ShoppingCart.GetCart(this.HttpContext);

            cart.AddToCart(addedAlbum, cart.ShoppingCartId);

            // Go back to the main store page for more shopping
            return RedirectToAction("Index");
        }


    }
}

Model Classes 模型类

namespace MusicStore.Core
{
    [Serializable]
    [DataContract]
    public class Cart
    {
        [Key]
        [DataMember]
        public int RecordId { get; set; }
        [DataMember]
        public string CartId { get; set; }
        [DataMember]
        public int AlbumId { get; set; }
        [DataMember]
        public int Count { get; set; }
        [DataMember]
        public System.DateTime DateCreated { get; set; }
        [DataMember]
        public virtual Album Album { get; set; }
    }
}

namespace MusicStore.Core
{
    [Serializable]
    [DataContract]
    //[Bind(Exclude = "AlbumId")]
    public class Album
    {
        [DataMember]
        [ScaffoldColumn(false)]
        public int AlbumId { get; set; }

        [DataMember]
        [DisplayName("Genre")]
        public int GenreId { get; set; }

        [DataMember]
        [DisplayName("Artist")]
        public int ArtistId { get; set; }

         [DataMember]
        [Required(ErrorMessage = "An Album Title is required")]
        [StringLength(160)]
        public string Title { get; set; }

         [DataMember]
        [Required(ErrorMessage = "Price is required")]
        [Range(0.01, 100.00,
            ErrorMessage = "Price must be between 0.01 and 100.00")]
        public decimal Price { get; set; }

         [DataMember]
        [DisplayName("Album Art URL")]
        [StringLength(1024)]
        public string AlbumArtUrl { get; set; }

           [DataMember]
         public virtual Genre Genre { get; set; }
           [DataMember]
        public virtual Artist Artist { get; set; }
         public virtual List<OrderDetail> OrderDetails { get; set; }
    }
}

DB Context 数据库上下文

namespace MusicStore.Data
{
    public class MusicStoreEntities : DbContext
    {
        public MusicStoreEntities()
            : base("MusicStoreEntities")
        {
            var ensureDLLIsCopied = System.Data.Entity.SqlServer.SqlProviderServices.Instance;

            this.Configuration.ProxyCreationEnabled = false;

            Database.SetInitializer<MusicStoreEntities>(new CreateDatabaseIfNotExists<MusicStoreEntities>());
            Database.SetInitializer(new CommonDBInitializer());
        }

        public DbSet<Album> Albums { get; set; }
        public DbSet<Genre> Genres { get; set; }
        public DbSet<Artist> Artists { get; set; }
        public DbSet<Cart> Carts { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<OrderDetail> OrderDetails { get; set; }
    }
}

Question is: why it is the Album object is not loaded inside Cart object, and how to fix it? 问题是:为什么没有在Cart对象内加载Album对象,该如何解决?

Entity Framework does not load related objects by default to help prevent potential performance problems around loading one-to-many or many-to-many relationships into memory. 实体框架默认情况下不会加载相关对象,以帮助防止在将一对多或多对多关系加载到内存中时潜在的性能问题。

Looking at the code you have posted, it seems a potential fix would be for you to add .Include("Album") when getting out the cart items in GetCartItems . 查看您发布的代码,似乎可能的解决方法是在GetCartItems取出购物车项目时添加.Include("Album") It would then become 然后它将变成

public static List<Cart> GetCartItems(string ShoppingCartID)
{
    using (MusicStoreEntities db = new MusicStoreEntities())
    {
        return db.Carts.Include("Album").Where(cart => cart.CartId == ShoppingCartID).ToList();
    }
}

See the Entity Framework docs for some other options around loading related entities 有关加载相关实体的其他一些选项,请参见实体框架文档

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM