0

Picking up from post: Define one-to-one in EF code first with fluent API where I had trouble getting a one-to-one working, I now have another problem with a one-to-many.

Here are my two classes:

[Table("PSCStatuses")]
public class PSCStatus
{
    [Key]
    public int PSCStatusID { get; set; }
    public string StatusCode { get; set; }
    public string StatusTextDesc { get; set; }
    public int NumDaysToProjEndDate { get; set; }

    public virtual List<Case> Cases { get; set; }
}

public class Case
{
    // Key: Tells EF this is the PK for Case.
    // ForeignKey("Appointee"): tells EF Appointee is the Principle/Parent in the 1:1
    [Required]
    [Key, ForeignKey("Appointee")]  
    public string ProfileID { get; set; }

    [Required]
    public int? PSCStatusID { get; set; }

    public virtual PSCStatus PSCStatus { get; set; }
    public virtual Appointee Appointee { get; set; }
}

You can see here what I had to do in my previous post to get Case to have one Appointee. (And Appointee to have a Case where Appointee is the Principle/Parent). I don't recall ever having to jump through hoops with EF before. But I think I am very rusty here.

Now after solving that I have a new problem. I can't get Case to fill in PSCStatus. When I inspect Case.PSCStatus at a break point after adding the case with the PSCstatusID set, I should see the PSCStatus object filled in and populated. But it remains null. I would think that the definition above would tell EF everything it needs to know but it is not working.

I also tried fluent API:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Case>()
                    .HasRequired<PSCStatus>(c => c.PSCStatus)
                    .WithMany(s => s.Cases)
                    .HasForeignKey(c => c.PSCStatusID);
    }
Community
  • 1
  • 1
Sam
  • 4,766
  • 11
  • 50
  • 76
  • How are you getting the object before you inspect it at a breakpoint? Are you making sure to eager load the PSCStatus? – DrewJordan Feb 11 '15 at 16:50

1 Answers1

0

Use: ICollection insead of List

[Edit]

If this doesn't work try this model builder:

modelBuilder.Entity<Case>()
        .HasRequired(c => c.PSCStatus)
        .WithMany(s => s.Cases)
        .HasForeignKey(c => c.PSCStatusID);
Claudio P
  • 2,133
  • 3
  • 25
  • 45
  • Also for lazy loading to work the type must implement `ICollection` and the access must be `public` or `protected`. – Nikolay Kostov Feb 11 '15 at 16:15
  • Thanks - I tried both and still no luck. I just don't know why EF is not listening to my navigation properties and foreign key attributes. – Sam Feb 11 '15 at 16:33
  • Is there a reason for the nullable foreign key in the model? Try remove the ? – Claudio P Feb 11 '15 at 16:43
  • Not marking this comment as answer because It's kind of half way there. But, at the break point if I pull the Case back out of the Case Repo, the PSCStatus property is set. Had a hunch that, yeah I added it, but the current Case I have hold of hasn't had the PSCStatus set yet. You have to go back out to the context and grab that one. – Sam Feb 11 '15 at 17:25