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?

aescript
  • 1,775
  • 4
  • 20
  • 30
  • 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)` – jaabh Oct 09 '20 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... – aescript Oct 09 '20 at 16:25

1 Answers1

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;
aescript
  • 1,775
  • 4
  • 20
  • 30
  • That's why you should put all of your "using" statements in your question. Too much ambiguity these days with just the classnames. #justSayin – granadaCoder Oct 09 '20 at 17:49
  • 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. – aescript Oct 09 '20 at 18:05
  • Haha. I do! :). See. https://stackoverflow.com/questions/53263204/dotnet-core-console-app-with-enterprise-library-data-enterpriselibrary-data-n. nuget package (and version) and usings. all listed out. – granadaCoder Oct 09 '20 at 18:59
  • You saved my life. I was hitting my head to the wall. I added System.Data.Entity, too! Thanks!! – Max Bertoli Dec 02 '21 at 08:53
  • Glad it was able to help somone @MaxBertoli – aescript Jan 05 '22 at 17:42