Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems when mapping a private property
    primarykey
    data
    text
    <p>I have two entity classes Project and Product with one-to-many association:</p> <pre><code>public class Product { public string Id {get; set;} public virtual Project Project {get; set;} } public class Project { public string Id {get; set;} protected virtual List&lt;Product&gt; Products {get; set;} public ReadOnlyCollection&lt;Product&gt; GetProducts() { return Products.AsReadOnly(); } public class PropertyAccessExpressions { public static Expression&lt;Func&lt;Project, ICollection&lt;Product&gt;&gt;&gt; Products = x =&gt; x.Products; } } public class MyDbContext: DbContext { public MyDbContext(string connectionString): base(connectionString){} public DbSet&lt;Project&gt; Projects {get; set;} public DbSet&lt;Product&gt; Products {get; set;} protected override void OnModelCreating(DbModelBuilder modelBuilder) { //// project.GetProducts() fails for the following configuratin //modelBuilder.Entity&lt;Product&gt;() // .HasRequired(p =&gt; p.Project).WithMany(Project.PropertyAccessExpressions.Products); // The following is OK modelBuilder.Entity&lt;Project&gt;() .HasMany(Project.PropertyAccessExpressions.Products).WithRequired(p =&gt; p.Project); } } class Program { static void Main(string[] args) { var context = new MyDbContext(@"data source=localhost;initial catalog=MyTestDb;integrated security=True;"); context.Database.Delete(); context.Database.Create(); var project1 = new Project { Id = "ProjectId1" }; context.Projects.Add(project1); context.Products.Add(new Product { Id = "ProductId1", Project = project1 }); context.Products.Add(new Product { Id = "ProductId2", Project = project1 }); context.SaveChanges(); var project = context.Projects.ToList()[0];; var products = project.GetProducts().ToList(); Debug.Assert(products.Count == 2); } } </code></pre> <p>To map the protected property, I use <a href="http://blog.cincura.net/232731-mapping-private-protected-properties-in-entity-framework-4-x-code-first/" rel="nofollow">this solution</a>.</p> <p>But I encountered the following problems:</p> <p>1) if I configure the one-to-many association between Project and Product with </p> <pre><code>modelBuilder.Entity&lt;Product&gt;.HasRequied(p =&gt; p.Project).WithMany(Project.PropertyAccessExpressions.Products); </code></pre> <p>Then the Project.GetProducts() fails and it seems the lazy loading does not work. But if I change to</p> <pre><code>modelBuilder.Entity&lt;Project&gt; .HasMany(Project.PropertyAccessExpressions.Products).WithRequired(p =&gt; p.Project); </code></pre> <p>Then everything is OK.</p> <p>2) If I change the property "Project.Products" from protected to <strong>public</strong>, then both of the above ways are OK.</p> <p>What's wrong in this situation?</p>
    singulars
    1. This table or related slice is empty.
    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