0

I have two model classes, experience and proposals.

Here is the code of experience

public class Experience : BaseEntity
{
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual DateTime? CreationDate { get; set; }
        public virtual string City { get; set; }
        public virtual double Price { get; set; }

        public virtual double? Lat { get; set; }
        public virtual double? Lng { get; set; }
        public virtual bool IsActive { get; set; }
        public virtual bool IsFinished { get; set; }
        public virtual bool IsConfirmed { get; set; }
        public virtual int CountryId { get; set; }
        [ForeignKey("CountryId")]
        public virtual Country Country { get; set; }
        public virtual string UserId { get; set; }
        [ForeignKey("UserId")]
        public virtual AppUser IdentityUser { get; set; }

        public virtual ICollection<Proposals.Proposals> Proposals { get; set; }
        public virtual ICollection<Comments.Comments> Comments { get; set; }
}

And Proposal

public class Proposals : BaseEntity
{
    public virtual string Description { get; set; }
    public virtual double Price { get; set; }
    public virtual bool IsActive { get; set; }
    public virtual bool IsConfirmed { get; set; }
    [ForeignKey("ExperienceId")]
    public virtual Experience Experience { get; set; }
    public virtual int ExperienceId { get; set; }
    public virtual string UserId { get; set; }
    [ForeignKey("UserId")]
    public virtual AppUser IdentityUser { get; set; }
}

Experience is related to User model

I need to get all Proposals related to experiences, that have user.

I wrote this method

  public async Task<List<ProposalsDto>> GetUserProposalCustomer(string id)
    {
        var proposals = await _context.Experiences.Where(x => x.UserId == id).Select(x => x.Proposals)
            .ProjectTo<ProposalsDto>().ToListAsync();
        return proposals;
    }

But it returns me this

[
{
    "description": null,
    "proposalPrice": 0.0,
    "experiencePrice": 0.0,
    "creator": null,
    "proposalMessage": null,
    "email": null,
    "experienceId": 0,
    "isActive": false,
    "userId": null,
    "isConfirmed": false,
    "id": 0
},
{
    "description": null,
    "proposalPrice": 0.0,
    "experiencePrice": 0.0,
    "creator": null,
    "proposalMessage": null,
    "email": null,
    "experienceId": 0,
    "isActive": false,
    "userId": null,
    "isConfirmed": false,
    "id": 0
},

I expect to have those values

enter image description here

Where is my trouble?

UPDATE

I tried to add .Include and I tried to write SQL Script

Here is it

 SELECT a.id
FROM dbo.Proposals AS a
INNER JOIN dbo.Experiences AS b ON a.ExperienceId=b.Id WHERE a.UserId = '3f4853a0-e310-44c2-a9e5-0c8d1dd7231e';

And I have all the values. But on API I have same stuff as before.

enter image description here

7
  • 1
    What is the expectation? Commented Jan 22, 2020 at 5:58
  • Not blank values @Yasser Commented Jan 22, 2020 at 5:59
  • 1
    how about Proposals value before you 'ProjectTo' it. I would check mappings Commented Jan 22, 2020 at 6:02
  • Can you try to see values in await _context.Experiences.Where(x => x.UserId == id).Select(x => x.Proposals) without the mapping part? Do you get the values here? Commented Jan 22, 2020 at 6:02
  • Have you tried debugging to see if you are getting the correct id parameter and then in proposals variable? Commented Jan 22, 2020 at 6:02

1 Answer 1

1

Do you have lazy loading enabled in your EF configuration?

Try to use Include to eager load the proposals.

var proposals = await _context
                        .Experiences
                        .Include(x => x.Proposals)
                        .Where(x => x.UserId == id)
                        .Select(x => x.Proposals).ToListAsync();
Sign up to request clarification or add additional context in comments.

2 Comments

Tried now, but have the same stuff.
Can you try to write a SQL query using same joins to see if you really have data present correctly?

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.