为什么我在Abstract类中遇到MissingMethodException

时间:2013-09-02 14:30:14

标签: asp.net-mvc ef-code-first

我有一个抽象类和其他继承它的类。

这些课程如下:

[Table("Contents", Schema="Admon")]
    public abstract class Content
    {
        public Content()
        {
            this.EntryDate = DateTime.Now;
        }
        [Key]
        public int ID { get; set; }
        public string Title { get; set; }
        public int? ParentID { get; set; }
        [StringLength(15)]
        public string InfoType { get; set; }
        public DateTime EntryDate { get; set; }
        public string Preview { get; set; }
        public string Description { get; set; }
        public string Link { get; set; }
        public string Text { get; set; }
        public string CategoryID { get; set; }
        public int? DocID { get; set; }

        public virtual Content Parent { get; set; }
        public virtual ICollection<Content> Subs { get; set; }
    }

    public class Photo : Content { }
    public class Notice : Content { }
    public class Article : Content { }
    public class Calendar : Content { }

我的问题是,无论何时我运行我的应用程序,它都会抛出一个读取

的异常
System.MissingMethodException: Cannot create an abstract class

我该怎么做才能纠正这个错误。

提前致谢

3 个答案:

答案 0 :(得分:1)

要在Entity Framework中使用继承,您必须实现TPH(每个层次表)或TPT(每个类型的表)数据库结构。

通过此策略,您将能够实现预期的行为。 您可以按照this article实施TPH或TPT,并了解此技术。

希望它有所帮助!

答案 1 :(得分:0)

您无法创建抽象类的实例。抽象类包含抽象成员,用于定义子类应包含的内容。

如果这个类必须保持抽象,你需要创建一个继承它的第二个类并实现它的成员并使用那个类来进行处理。

如果类不必保持抽象(我不明白为什么它应该是但没有看到你的其余代码我不能100%确定)然后只需删除abstract关键字。

答案 2 :(得分:0)

内容类无法编译,因为您的班级是抽象。 MVC引擎 无法直接创建内容实例。除非你用某种方式知道要实例化哪种类型的问题,否则它无能为力。        可以创建自己的ModelBinder实现,并告诉MVC使用它。例如,您的实现可以绑定到依赖注入框架,以便在请求内容类时知道创建 Content1