EF 5.0 Code First navigation property null for only one property

I seem to be having a problem with the following model using EF 5.0 CodeFirst:

When I try and do a listing with SalesPlanActivity I am unable to get the Activity name from the navigational property

i.e. If my model is SalesPlanActivity, model.Activity <--- is null, yet model.Company and model.SalesPlan are fine.

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)

Am I mapping this incorrectly?

Should the mapping between SalesPlanActivity and Activity be 1:1?

I've added the following relationships

 // Relationships
        modelBuilder.Entity<SalesPlanActivity>().HasRequired(t => t.Activity)
            .WithMany(t => t.SalesPlanActivities)                
            .HasForeignKey(d => new { d.CompanyId, d.ActivityId });

        modelBuilder.Entity<SalesPlanActivity>().HasRequired(t => t.SalesPlan)
            .WithMany(t => t.SalesPlanActivities)
            .HasForeignKey(d => 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<SalesPlanActivity> 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<Activity> Activities { get; set; }
    public virtual ICollection<SalesPlan> 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<SalesPlanActivity> 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; }
}

Answers


Need Your Help

iterm gnu bash visual corruption

bash gnu ncurses curses iterm

When i paste a command in to bash and hit enter, the next lines prompt contains half of the previous issued command. E.G:

Some commands hang my Ruby web app

ruby multithreading sinatra command-execution

I'm playing around with Rails &amp; Sinatra, and I want to execute commands on the server. Those commands are entered from a form. The thing is, if I enter a command which expects input, my whole app

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.