Note that there are some explanatory texts on larger screens.

plurals
  1. POEF 5.0 Code First navigation property null for only one property
    primarykey
    data
    text
    <p>I seem to be having a problem with the following model using EF 5.0 CodeFirst:</p> <p><img src="https://i.stack.imgur.com/dpp9e.png" alt="enter image description here"></p> <p>When I try and do a listing with SalesPlanActivity I am unable to get the Activity name from the navigational property</p> <p>i.e. If my model is SalesPlanActivity, model.Activity &lt;--- is null, yet model.Company and model.SalesPlan are fine.</p> <p>The idea is that a company has a library of activities, each company has a number of salesplans, each salesplans has a number of activities (which come from the activity library) </p> <p><strong>Am I mapping this incorrectly?</strong></p> <p><strong>Should the mapping between SalesPlanActivity and Activity be 1:1?</strong></p> <p>I've added the following relationships</p> <pre><code> // Relationships modelBuilder.Entity&lt;SalesPlanActivity&gt;().HasRequired(t =&gt; t.Activity) .WithMany(t =&gt; t.SalesPlanActivities) .HasForeignKey(d =&gt; new { d.CompanyId, d.ActivityId }); modelBuilder.Entity&lt;SalesPlanActivity&gt;().HasRequired(t =&gt; t.SalesPlan) .WithMany(t =&gt; t.SalesPlanActivities) .HasForeignKey(d =&gt; new { d.SalesPlanId, d.CompanyId }); public class Activity : IAuditable { [Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ActivityId { get; set; } [Key, Column(Order = 1)] public int CompanyId { get; set; } public virtual Company Company { get; set; } [Display(Name = "Activity Name"), Required, StringLength(256)] public string Name { get; set; } public virtual ICollection&lt;SalesPlanActivity&gt; SalesPlanActivities { get; set; } [DataType(DataType.DateTime)] public DateTime CreatedOn { get; set; } public Guid CreatedBy { get; set; } [DataType(DataType.DateTime)] public DateTime ModifiedOn { get; set; } public Guid ModifiedBy { get; set; } } public class Company : IAuditable { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int CompanyId { get; set; } [Display(Name = "Company Name"), Required, StringLength(256)] public string Name { get; set; } public virtual ICollection&lt;Activity&gt; Activities { get; set; } public virtual ICollection&lt;SalesPlan&gt; SalesPlans { get; set; } [DataType(DataType.DateTime)] public DateTime CreatedOn { get; set; } public Guid CreatedBy { get; set; } [DataType(DataType.DateTime)] public DateTime ModifiedOn { get; set; } public Guid ModifiedBy { get; set; } } public class SalesPlan : IAuditable { [Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int SalesPlanId { get; set; } [Key, Column(Order = 1)] public int CompanyId { get; set; } public virtual Company Company { get; set; } [Display(Name = "Sales Plan Name"), Required, StringLength(256)] public string Name { get; set; } [Display(Name = "Total Points"), Required] public decimal TotalPoints { get; set; } public virtual ICollection&lt;SalesPlanActivity&gt; SalesPlanActivities { get; set; } [DataType(DataType.DateTime)] public DateTime CreatedOn { get; set; } public Guid CreatedBy { get; set; } [DataType(DataType.DateTime)] public DateTime ModifiedOn { get; set; } public Guid ModifiedBy { get; set; } } public class SalesPlanActivity : IAuditable { [Key, Column(Order = 0)] public int SalesPlanId { get; set; } public virtual SalesPlan SalesPlan { get; set; } [Key, Column(Order = 1)] public int CompanyId { get; set; } public virtual Company Company { get; set; } [Key, Column(Order = 2)] public int ActivityId { get; set; } public virtual Activity Activity { get; set; } public decimal Points { get; set; } public int SortOrder { get; set; } [DataType(DataType.DateTime)] public DateTime CreatedOn { get; set; } public Guid CreatedBy { get; set; } [DataType(DataType.DateTime)] public DateTime ModifiedOn { get; set; } public Guid ModifiedBy { get; set; } } </code></pre>
    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.
    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