Note that there are some explanatory texts on larger screens.

plurals
  1. POLazy Loading questions with EF5 and Code First
    primarykey
    data
    text
    <p>It seems that lazy loading does not work the same with code-first as it does with db-first:</p> <p>Consider:</p> <pre><code>public class Project { [Key] public long ProjectId { get; set; } [Required] [MaxLength(60)] public virtual string Name { get; set; } } </code></pre> <p>And:</p> <pre><code>public class TestCase { [Key] public long TestCaseId { get; set; } [Required] [ForeignKey("ProjectId")] public virtual Project Project { get; set; } public virtual long ProjectId { get; set; } } </code></pre> <p>In this DbContext:</p> <pre><code>public class TestDbContext : DbContext { public TestDbContext() : base("TestIckle") { } public DbSet&lt;Project&gt; Projects { get; set; } public DbSet&lt;TestCase&gt; TestCases { get; set; } } </code></pre> <p>When I try to to this in my code it does not work:</p> <pre><code> Console.WriteLine(_db.Configuration.LazyLoadingEnabled); //prints true TestDbContext _db = new TestDbContext(); var testcase = _db.TestCases.Create(); testcase.Project = _db.Projects.Find(7); //the following line outputs 0 with Code-First //the following line outputs 7 with DB-First Console.WriteLine(testcase.ProjectId); </code></pre> <p>Is a code-first model less capable than a db-first one? Seems odd that I would have assign the nav property and the pkey for it, which makes me think I have something configured wrong.</p> <p>Should the following ever work?</p> <pre><code>var x = _db.TestCases.Create(); x.ProjectId = 7; Console.WriteLine(x.Project.Name); //this never seems to work </code></pre> <p>Would be nice, seing as x knows which context it is attached too, I would have thought it could have handled this situation.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload