0

我有以下课程:

public class Page : BaseModel
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int PageID { get; set; }
    public int? ParentID { get; set; }
    public string Name { get; set; }

    public virtual Page Parent { get; set; }
    public virtual ICollection<Page> Children { get; set; }

    public IDictionary<int, string> PossibleParents
    {
        get
        {
            Dictionary<int, string> items = new Dictionary<int, string>();
            items.Add(-1, "Please Select");

            using (var context = new WebsiteContext())
            {
                var pages = context.Pages;
                foreach (var p in pages)
                    items.Add(p.PageID, p.Name);                    
            }

            return items;
        }
    }

    public Page()
    {
        this.Order = 0;
        this.Live = false; 
    }
}

public class CommodityPageMap : EntityTypeConfiguration<Page>
{
    public CommodityPageMap()
    {
        HasOptional(x => x.Parent)
            .WithMany(x => x.Children)
            .HasForeignKey(x => x.ParentID)
            .WillCascadeOnDelete(false);
    }
}

问题

我假设它将映射ParentID以将页面链接到父页面。然而,似乎在更新数据库时 EF 已创建Parent_PageID. 现在,当我设置ParentID它时,它没有正确创建关联并且Parents不包含任何元素,因为Parent_PageID它为空。

我努力了

如果我删除ParentID,那么它将无法编译,因为它需要设置外键。我无法将 Data Annotation 添加[Column("Parent_PageID")]到,ParentID因为这会在访问时在运行时产生错误,并且添加到我的模型中并忽略PossibleParents似乎很愚蠢。Parent_PageIDParentID

我想我错过了一些东西,但希望能得到一些帮助。

编辑

我刚刚尝试添加以下内容:

public int? ParentID { get { return this.Parent_PageID; } set { this.Parent_PageID = value; } }
public int? Parent_PageID { get { return this.ParentID; } set { this.Parent_PageID = value; } }

但这会Sequence contains no elements在尝试访问时引发PossibleParents

4

1 回答 1

0

尽管我不知道为什么使用 Fluent API 会导致问题,但我设法解决了我的问题。

我更改了我的代码替换ParentIDParent_PageID. 然后我将[ForeignKey("Parent")]数据注释添加到Parent_PageID. 我的代码最终是这样的:

public class Page : BaseModel
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int PageID { get; set; }
    [ForeignKey("Parent")]
    public int? Parent_PageID { get; set; }
    public int Order { get; set; }
    public bool Live { get; set; }
    [Required]
    [MaxLength(255)]
    public string Name { get; set; }
    public string SEOTitle { get; set; }
    public string SEODescription { get; set; }
    public string SEOKeywords { get; set; }
    [AllowHtml]
    public string Content { get; set; }

    public virtual Page Parent { get; set; }
    public virtual ICollection<Page> Children { get; set; }

    public IDictionary<int, string> PossibleParents
    {
        get
        {
            Dictionary<int, string> items = new Dictionary<int, string>();
            items.Add(-1, "Please Select");

            using (var context = new WebsiteContext())
            {
                var pages = context.Pages;
                foreach (var p in pages)
                    items.Add(p.PageID, p.Name);                    
            }

            return items;
        }
    }

    public Page()
    {
        this.Order = 0;
        this.Live = false; 
    }
}
于 2013-09-29T16:20:21.327 回答