简体   繁体   中英

EF Core NullReferenceException on Related Navigation Property

I have two related models.

    public class Offer
    {
        public long Id { get; set; }
        public string OfferCode { get; set; }
        public string Description { get; set; }

        // more properties

        public int ProductId { get; set; }
        public virtual Product Product { get; set; }
    }

    public class Product
    {
        public long Id { get; set; }
        public string Name { get; set; }

        // more properties

        public virtual ICollection<Offer> Offers { get; set; }
    }

I am trying to have an MVC form with a select HTML element where Offers are grouped and products and have the Product Names serve as optgroups. To this end, I have a view model that I intend to populate with the grouped Offers and I have a method to do just that.

        private OfferMessageViewModel PrepareViewModel(OfferMessageViewModel viewModel)
        {
            var offers = _context.Offers.Include(o => o.Product).ToList()
                .GroupBy(o => o.Product.Name).ToList();

            foreach (var offerGroup in offers)
            {
                var optionGroup = new SelectListGroup
                {
                    Name = offerGroup.Key
                };

                foreach (var offer in offerGroup)
                {
                    viewModel.Offers.Add(
                        new SelectListItem
                        {
                            Value = offer.OfferCode,
                            Text = offer.Description,
                            Group = optionGroup
                        }
                    );
                }
            }
            return viewModel;
        }

The code gets tripped up in the GroupBy clause. o.Product is null even when o.ProductID has a value in it.

The ToList() call right before the GroupBy is not helping.

I have tried removing the virtual modifiers on the related entities navigation properties but the error persisted.

Installing the NuGet package Microsoft.EntityFrameworkCore.Proxies and modifying and configuring it as such

services.AddDbContext<ApplicationDbContext>(options =>
                options.UseLazyLoadingProxies() 
                .UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));

also did not make the error go away.

Is there something else I am missing?

Any help would be greatly appreciated.

EDIT: It has been suggested that my post might be solved by this SO question . But I get the null reference exception even with lazy loading explicitly turned on. I have tried the suggested solutions there but still no luck.

I eventually solved it.

Apparently the problem was that the foreign key was an int referencing a primary key of type long .

So I changed

public int ProductId { get; set; }

to

public long ProductId { get; set; }

in the Offer model. Added the necessary migration, updated the database and now it works. No more null reference exceptions.

Don't know why I missed that but it's probably a combination of lack of sleep and a not-so-helpful error message throwing me off in a completely different direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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