Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8079629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:10:03+00:00 2026-06-05T16:10:03+00:00

All my code is here,quite simple,and I don’t konw where it goes wrong. Person

  • 0

All my code is here,quite simple,and I don’t konw where it goes wrong.

Person and Task has an many-to-many relationship.
I want to load someone’s task using the explicit way.
I follow the way this post shows,and i can’t make it work.

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Task> Tasks { get; set; }
}
public class Task
{
    public int Id { get; set; }
    public string Subject { get; set; }
    public ICollection<Person> Persons { get; set; }

}

public class Ctx : DbContext
{
    public Ctx()
        : base("test")
    {
        this.Configuration.LazyLoadingEnabled = false;
        this.Configuration.ProxyCreationEnabled = false;
    }
    public DbSet<Person> Persons { get; set; }
    public DbSet<Task> Task { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        //add some data as follows
        //using (var ctx = new Ctx())
        //{

            //ctx.Persons.Add(new Person { Name = "haha" });
            //ctx.Persons.Add(new Person { Name = "eeee" });

            //ctx.Task.Add(new Task { Subject = "t1" });
            //ctx.Task.Add(new Task { Subject = "t2" });
            //ctx.SaveChanges();

            //var p11 = ctx.Persons.FirstOrDefault();
            //ctx.Task.Include(p2 => p2.Persons).FirstOrDefault().Persons.Add(p11);
            //ctx.SaveChanges();
        //}

        var context = new Ctx();
        var p = context.Persons.FirstOrDefault();

        context.Entry(p)
        .Collection(p1 => p1.Tasks)
        .Query()
        //.Where(t => t.Subject.StartsWith("t"))
        .Load();

        //the tasks should have been loaded,isn't it?but no...
        Console.WriteLine(p.Tasks != null);//False

        Console.Read();
    }
}

Is there anything wrong with my code?I’m really new to EF,so please, someone help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:10:05+00:00Added an answer on June 5, 2026 at 4:10 pm

    The problem is your .Query() call. Instead of loading the collection, you are getting a copy of the IQueryable that would be used to load, then executing the query.

    Remove your .Query() line and it will work.


    If what you are looking for is getting a filtered list of collection elements, you can do this:

    var filteredTasks = context.Entry(p)
                               .Collection(p1 => p1.Tasks)
                               .Query()
                               .Where(t => t.Subject.StartsWith("t"))
                               .ToList();
    

    This will not set p.Tasks, nor is it a good idea to do so, because you’d be corrupting the domain model.

    If you really, really want to do that… this might do the trick (untested):

    var collectionEntry = context.Entry(p).Collection(p1 => p1.Tasks);
    collectionEntry.CurrentValue =
        collectionEntry.Query()
                       .Where(t => t.Subject.StartsWith("t"))
                       .ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

enter code here Hi All, I have a simple windows service application that connects
here is the code: http://jsfiddle.net/KTHWd/ so basically all divs has same class and i
All the code is inline here: http://shanxinc.com/test/fancybox2/ All I'm trying to do is get
I've found some code on here that lists all the USB devices on a
HI Here is my code: Will this code go into all the pages of
So here is the code using S.DS.P to get all users very quickly in
here is my custom code this will list all page from specific parent but
Here is my code: var items = tableInDatabase.All(WHERE [Order] > + order); foreach (var
I've tried everything. Arrays are quite simple so I don't know why this doesn't
I am trying to complete a seemingly simple task that has turned into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.