2

Thanks for the replies, to be more clare, i have re-orged my question as follows:

 public class App
{
    [Key]
    public Guid Id { get; set; }

    public virtual IEnumerable<Build> Builds { get; set; }
}

public class Build
{
    [Key]
    public Guid Id { get; set; }

    public Guid AppId { get; set; }

    [ForeignKey("AppId")]
    public virtual App App { get; set; }
}


public class TestContext : DbContext
{
    public TestContext() : base("name=DefaultConnection")
    {
        Database.SetInitializer<TestContext>(new CreateDatabaseIfNotExists<TestContext>());
    }

    public DbSet<App> Apps { get; set; }
    public DbSet<Build> Builds { get; set; }
}

Step 1> Save Data

 class Program
{
    static void Main(string[] args)
    {
        using (TestContext context = new TestContext())
        {
            var id = Guid.NewGuid();
            App app = new App() { Id = id };
            Build build = new Build()
            {
                Id = Guid.NewGuid(),
                AppId = id
            };
            context.Apps.Add(app);
            context.Builds.Add(build);
            context.SaveChanges();;
        }

    }
}

Step 2> Get Data

class Program
{
    static void Main(string[] args)
    {
        using (TestContext context = new TestContext())
        {
            var builds = context.Apps.FirstOrDefault().Builds;
            Console.Read();
        }
    }
}

The var builds get a null value, while i check database, the foreign key value is well saved.

Wei Yang
  • 141
  • 1
  • 11
  • what is your `EF query` for that ? put that code also. – Sampath Sep 20 '16 at 16:03
  • I'm not sure how this is set up. Is there a reason You have your Build table represented by a class named `App`? Is that just a typo? Then what's AppBuild? – KSib Sep 20 '16 at 16:08
  • a simple query >> app.Builds – Wei Yang Sep 20 '16 at 16:32
  • @Ksib, not a typo, just want to do some test, i have updated my question, now it is more clare, thanks – Wei Yang Sep 20 '16 at 16:51
  • To be honest, I'm not sure how your Step 1 is saving anything to the database. Should be `context.App.Add(app);` before your `.SaveChanges()` – KSib Sep 20 '16 at 16:55

2 Answers2

1

My guess is that you are using eager loading and haven't specified an Include() for that property (likely), which would look something like

context.Apps.Where(...).Include(a => a.Builds).ToList()

or you are using explicit loading and haven't called Load() (less likely).

Kit
  • 20,354
  • 4
  • 60
  • 103
  • App app = new App(){ id = 1 ... }; Build build = new Build { .. AppId = 1} var builds = app.Builds(); and builds = null; – Wei Yang Sep 20 '16 at 16:23
  • That's not a query. That's just code constructing your object graph. – Kit Sep 20 '16 at 16:38
0

It's probably that your local EF hasn't been refreshed with App's new collection. You may need to refresh the App object from your database.

How to Refresh DbContext

Community
  • 1
  • 1
drovani
  • 928
  • 1
  • 17
  • 37
  • wantna to know whether only set the AppId property in Build object works? i have restarted VS servral times, still not work – Wei Yang Sep 20 '16 at 16:12
  • Setting the AppBuild.AppId and then persisting that object to the database should allow you to query the App.Builds property. The key, though, is that you need to save the changes to your database, and then refresh your data. Restarting Visual Studio will not do anything. – drovani Sep 20 '16 at 16:15