我如何在 .net 教程中使用抽象和具体模型?

How do i work with abstract and concrete models in .net tutorial?

我正在关注 this tutorial,在我将模型中的 Person 实体设置为 abstract 之后,我收到以下错误:

Type 'SchoolModel.Person' in conceptual side cannot be mapped to type 'ContosoUniversity2.DAL.Person' on the object side. Both the types must be abstract or both must be concrete types.

我认为我需要更改 Person.cs 并使其抽象化。 更改文件后,我再次 运行 并得到:

Could not find the CLR type for 'SchoolModel.Student'

我正在使用 Visual Studio 2012。有什么解决办法吗?

Person.cs 的内容:

namespace ContosoUniversity2.DAL{
using System;
using System.Collections.Generic;

public partial class Person
{
    public Person()
    {
        this.StudentGrades = new HashSet<StudentGrade>();
        this.Courses = new HashSet<Course>();
    }

    public int PersonID { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public Nullable<System.DateTime> HireDate { get; set; }
    public Nullable<System.DateTime> EnrollmentDate { get; set; }

    public virtual OfficeAssignment OfficeAssignment { get; set; }
    public virtual ICollection<StudentGrade> StudentGrades { get; set; }
    public virtual ICollection<Course> Courses { get; set; }
}
}

我的 Students.aspx 中的一段代码出现错误

 <asp:GridView ID="SearchGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="PersonID"
    DataSourceID="SearchEntityDataSource" AllowPaging="true">
    <Columns>
        <asp:TemplateField HeaderText="Name" SortExpression="LastName, FirstName">
            <ItemTemplate>
                <asp:Label ID="LastNameFoundLabel" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>, 
                <asp:Label ID="FirstNameFoundLabel" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Enrollment Date" SortExpression="EnrollmentDate">
            <ItemTemplate>
                <asp:Label ID="EnrollmentDateFoundLabel" runat="server" Text='<%# Eval("EnrollmentDate", "{0:d}") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

如果您需要更多代码,请询问,我会更新。 提前致谢!

您需要做的第一件事是将 entity framework 更新到最新版本,以便您可以使用所有最新功能。

您需要了解的另一件事是 Entity Framework 中的继承策略。

Table 每个层次结构 (TPH): 这种方法建议对整个 class 继承层次结构使用一个 table。 Table 包含区分继承 class 的鉴别器列。这是Entity Framework.

中的默认继承映射策略

Table 每个类型 (TPT): 这种方法建议为每个域单独 table class。

Table 每个混凝土 class (TPC): 这种方法建议一个 table 一个混凝土 class,但是不适用于摘要 class。因此,如果您在多个具体 class 中继承抽象 class,那么抽象 class 的属性将成为具体 class 的每个 table 的一部分。

看到 link 作为底部,因为他们已经使用 entity framework 6 和摘要 class。

 public abstract class Person
{
    public int ID { get; set; }

    [Required]
    [StringLength(50)]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }
    [Required]
    [StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]
    [Column("FirstName")]
    [Display(Name = "First Name")]
    public string FirstMidName { get; set; }

    [Display(Name = "Full Name")]
    public string FullName
    {
        get
        {
            return LastName + ", " + FirstMidName;
        }
    }
}

http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application