0

not sure what I've done wrong. I cannot, no matter what I do, and what posts I try to follow, get EF Eager loading to work. I don't know If I have done something wrong well before hand... but nothing works. Not any style of include, or disabling lazy loading... i just cannot get it.

I have an Agent model

public class Agent
    {
        [Key]
        public int AgentId { get; set; }     
        [Required]
        [StringLength(50)]    
        public string Username { get; set; }
        [Required]
        [StringLength(100)]
        public string FirstName { get; set; }
        [Required]
        [StringLength(100)]
        public string LastName { get; set; }

Then a prducer model

    public class Producer
    {

        [Key]
        public int ProducerId { get; set; }

        [Required]
        [StringLength(254)]
        public string Name { get; set; }
        
        public int StreetNumber { get; set; }
        [StringLength(254)]
        public string StreetName { get; set; }
        [StringLength(254)]
        public string City { get; set; }
        public int StateId { get; set; }
        public virtual State State { get; set; }
        public int CountryId { get; set; }
        public virtual Country Country { get; set; }

And then i have another model/table to link the two

    public class AgentProducer
    {
        public int AgentProducerId { get; set; }
        public int ProducerId { get; set; }
        public int AgentId { get; set; }
        public virtual Producer Producer { get; set; }
        public virtual Agent Agent { get; set; }
    }

The goal here, would be that when i Query thr AgentProducer table, my Producer property would have the entire producer object, and that producer object would have the Country and State objects.

I dont think this matters but the producer does have FK constraints on the countryId and stateId

My setup was from a lesson i was following, where i have a repository wrapper holding everything


    public class RepositoryContext : DbContext
    {
        public RepositoryContext(DbContextOptions<RepositoryContext> options) : base(options)
        {

        }
        public DbSet<Producer> Producers { get; set; }  
        public DbSet<Agent> Agents { get; set; }  
    }

The above item is held in a repository wrapper, that has a repository implementing a specific interface for each model type in it.

The problem Im having is whether i query with method syntax or LINQ, i cannot get everything to be included in the query, and end up having to do a bunch of extra steps to get certain info.

So far the closest i got was with LINQ, in which I would get the AgentProducer item to return with the Producer object set - however inside that producer, the COuntry and State were null, and I need that info.

I tried things like: *** (Worth noting "AgentProducer" here is a DBSet... not sure if thats correct or not? I dont seem to have options like "ThenInclude" that ive seen in other answers.

_repoWrapper.Repo.AgentProducer.Include(i => i.Producer).Where(i => i.AgentId == filter.AgentId);

This gives me absolutely nothing - even the producer is null - before even making it to my country/state problem.

I tried

            var res = _repoWrapper.Repo.AgentProducer.Include("Producer").Where(i => i.AgentId == filter.AgentId);

Same null result.

(from ap in _repoWrapper.Repo.AgentProducer.Include(i => i.Producer)
                       where ap.AgentId == filter.AgentId
                       select ap);

Same null.

The only thing that has even minorly worked was:

            var res = (from ap in _repoWrapper.Repo.AgentProducer
                       join p in _repoWrapper.Repo.Producers on ap.ProducerId equals p.ProducerId
                       join a in _repoWrapper.Repo.Agents on ap.AgentId equals a.AgentId
                       join c in _repoWrapper.Repo.Country on ap.Producer.Country.Name equals c.Name
                       join s in _repoWrapper.Repo.State on ap.Producer.State.Name equals s.Name
                       where ap.AgentId == filter.AgentId
                       orderby p.Name descending
                       select new AgentProducer
                       {
                           AgentProducerId = ap.AgentProducerId,
                           Producer = p
                       });

Which fills out the producer, because of the manual join and set on the object. However, A) This isnt really eager loading, and B) using this method I have no idea how i can set the country and state objects on the producer here, as they still show null. And in the object initializer i cant just assign the country and state.

Ive browsed countless approaches and I cannot get a single thing to work... so i feel like I have done something wrong earlier in the process but I have no clue what.

Can anyone help me out?

2
  • Assuming that Country and State are tables in the repository, then you could use a .ThenInclude(x => x.Country) and .ThenInclude(x => x.State) after the .Include(Producer) Commented Oct 9, 2020 at 16:18
  • The problem is the Include(Producer) doesnt work in the first place, and I dont even have the OPTION to run ThenInclude, it doesnt exist. The whole point of this post is that include has never worked for me no matter what I try... Commented Oct 9, 2020 at 16:25

1 Answer 1

2

Looks like it was me being a tool.

I had not grabbed the nuget package for EntityFrameworkCore.Relational, and had not added

using Microsoft.EntityFrameworkCore;

instead I had:

using System.Data.Entity;
Sign up to request clarification or add additional context in comments.

5 Comments

That's why you should put all of your "using" statements in your question. Too much ambiguity these days with just the classnames. #justSayin
You know, i 100% agree with you and have always thought the same, but no one here ever does... so i didn't. And look what happened.
Haha. I do! :). See. stackoverflow.com/questions/53263204/…. nuget package (and version) and usings. all listed out.
You saved my life. I was hitting my head to the wall. I added System.Data.Entity, too! Thanks!!
Glad it was able to help somone @MaxBertoli

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.