我在使用MVC C#时有一个基本类:
public class Page
{
//Bunch of files with Annotations
[Display(Name = "Page Type")]
[DisplayFormat(NullDisplayText = "N/A")]
public Guid? PageTypeID { get; set; }
[ForeignKey("PageTypeID")]
public virtual PageType PageType { get; set; }
}
当我构建Scaffolding时,以下代码将产生错误:
There was an error running the selected code generator:
'Unable to retrieve metadata from 'MyNamespace.Page'.'
我重建,清理,重启VS,没有任何帮助。
删除以下代码
[ForeignKey("PageTypeID")]
将允许创建脚手架,但我的Drop Down是一个文本字段。
我也有这个设置,似乎有效,对我没有意义,所以我希望更好地理解。用PageType替换DropDownItem仍然会产生与上面相同的错误。
[Display(Name = "Drop Down Item")]
[DisplayFormat(NullDisplayText = "N/A")]
public Guid? DropDownItemID { get; set; }
[ForeignKey("DropDownItemID")]
public virtual DropDownItem DropDownItem { get; set; }
PageType类如下:
public class PageType : DropDownItem
{
public new string Name { get; set; }
}
DropDownItem如下:
public class DropDownItem : BaseObject
{
[Required]
public string Name { get; set; }
}
我正在使用EntityFramework 6.1.3并被我的项目
引用我相信我在ApplicationDBContext
中缺少Get / Set of PageType在
public class ApplicationDBContext : DbContext
{
public ApplicationDBContext() : base("name=DefaultConnection") { }
public System.Data.Entity.DbSet<Site> Sites { get; set; }
public System.Data.Entity.DbSet<Page> Pages { get; set; }
public System.Data.Entity.DbSet<DropDownItem> DropDownItems { get; set; }
}
之后(刚添加此行)
public System.Data.Entity.DbSet<PageType> PageTypes { get; set; }
纠正我,如果我错了,但我相信这对我不起作用,因为上面的代码行(DbSet)不在那里,它不存在因为我忘了为它添加一个Controller(即:PageTypesController),从那里,我就可以将PageType添加到Page对象。