繁体   English   中英

一对多无法先获得EF代码

[英]Can't get EF code first one-to-many working

从帖子中接起: 首先用流畅的API在EF代码中定义一对一 ,但我无法进行一对一的工作,而现在我又遇到了一对多的问题。

这是我的两个班级:

[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; }
}

你可以在这里看到我在上一篇文章中要做的事情,让凯斯有一个被任命的人。 (并且被任命为受理人是原则/父母的案例)。 我不记得曾经不得不用EF跳过篮球。 但是我觉得我在这里很生锈。

现在解决之后,我遇到了一个新问题。 我无法让Case填写PSCStatus。 在添加设置了PSCstatusID的案例后,在断点处检查Case.PSCStatus时,我应该看到PSCStatus对象已填充并填充。 但它仍然是空的。 我认为上面的定义将告诉EF它需要知道的所有信息,但它不起作用。

我也尝试过流畅的API:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Case>()
                    .HasRequired<PSCStatus>(c => c.PSCStatus)
                    .WithMany(s => s.Cases)
                    .HasForeignKey(c => c.PSCStatusID);
    }

用途: List ICollection insead

[编辑]

如果这不起作用,请尝试以下模型构建器:

modelBuilder.Entity<Case>()
        .HasRequired(c => c.PSCStatus)
        .WithMany(s => s.Cases)
        .HasForeignKey(c => c.PSCStatusID);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM