如何在mvc4中使用外键关系

时间:2014-04-27 13:01:25

标签: c# asp.net-mvc-3 asp.net-mvc-4

我的数据库中有SchoolTeachersQualifications个表。 Teacher表格qualificationidschoolid以及Qualification表中的Schools

我没有使用Entity Framework,我有DB连接类来进行CLUED操作。

在我的Teacher班级

public int TeacherID { get; set; }
public string Name { get; set; }
public string NIC { get; set; }
public string Address { get; set; }
public int Telephone { get; set; }

public Nullable<int> SchoolID { get; set; }
public Nullable<int> QualificationID { get; set; }

public virtual Qualifications Qualifications { get; set; }
public virtual Schools Schools { get; set; }

我的schools班级

public int SchoolID { get; set; }
public string Name { get; set; }
public int Telephone { get; set; }
public string Address { get; set; }

public virtual ICollection<Teachers> Teachers { get; set; }

我的Qualification班级

public int QualificationID { get; set; }
public string Type { get; set; }

public virtual ICollection<Teachers> Teachers { get; set; }

我的DBConnection班级

public List<Teachers> getAllTeachers()
        {
            List<Teachers> teachersdata=new List<Teachers>();
            string connection = Connection.ConnectionString;
            SqlConnection con = new SqlConnection(connection);
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = "Select * from Teachers as T,Qualifications as Q,Schools as S  where S.SchoolID=T.SchoolID and T.QualificationID=Q.QualificationID";
        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.KeyInfo);
        while(dr.Read())
        {
            Teachers teacher = new Teachers();
            teacher.TeacherID = dr.GetInt32(0);
            teacher.Name = dr.GetString(1);
            teacher.NIC = dr.GetString(2);
            teacher.Address = dr.GetString(3);
            teacher.Telephone = dr.GetInt32(4);
            teacher.SchoolID = dr.GetInt32(5);
            teacher.QualificationID = dr.GetInt32(6);
            teacher.Qualifications.QualificationID = (Int32)dr["QualificationID"];
            teacher.Qualifications.Type =(string) dr["Type"];
            teacher.Schools.SchoolID = (Int32)dr["SchoolID"];
            teacher.Schools.Name = (string)dr["Name"];
            teacher.Schools.Telephone = (Int32)dr["Telephone"];
            teacher.Schools.Address = (string)dr["Address"];
            teachersdata.Add(teacher);
            }
            return teachersdata;
        }
teachersController

中的

public ActionResult Index()
        {
            List<Teachers> allTeachers = _db.getAllTeachers();
            return View(allTeachers);
        }

最后在index.aspx中我有

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MyLayer.Teachers>>" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <p>
        <%: Html.ActionLink("Create New", "Create") %>
    </p>
    <table>
        <tr>
            <th>
                <%: Html.DisplayNameFor(model => model.TeacherID) %>
            </th>
            <th>
                <%: Html.DisplayNameFor(model => model.Name) %>
            </th>
            <th>
                <%: Html.DisplayNameFor(model => model.NIC) %>
            </th>
            <th>
                <%: Html.DisplayNameFor(model => model.Address) %>
            </th>
            <th>
                <%: Html.DisplayNameFor(model => model.Telephone) %>
            </th>
            <th>
                <%: Html.DisplayNameFor(model => model.Schools.Name) %>
            </th>
            <th>
                <%: Html.DisplayNameFor(model => model.Qualifications.Type) %>
            </th>
            <th></th>
        </tr>

    <% foreach (var item in Model) { %>
        <tr>
            <td>
                <%: Html.DisplayFor(modelItem => item.TeacherID) %>
            </td>
            <td>
                <%: Html.DisplayFor(modelItem => item.Name) %>
            </td>
            <td>
                <%: Html.DisplayFor(modelItem => item.NIC) %>
            </td>
            <td>
                <%: Html.DisplayFor(modelItem => item.Address) %>
            </td>
            <td>
                <%: Html.DisplayFor(modelItem => item.Telephone) %>
            </td>
            <td>
                <%: Html.DisplayFor(modelItem => item.Schools.Name) %>
            </td>
            <td>
                <%: Html.DisplayFor(modelItem => item.Qualifications.Type) %>
            </td>
            <td>
                <%: Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> |
                <%: Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) %> |
                <%: Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ }) %>
            </td>
        </tr>
    <% } %>

    </table>
</body>
</html>

但是当我运行项目时,我得到了这个错误,对象引用没有设置为dbconnnection类teacher.Qualifications.QualificationID = (Int32)dr["QualificationID"];中的对象实例。我可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我使用此代码

进行了LINQPad程序
void Main()
{   
    Teachers teacher = new Teachers();
    teacher.TeacherID = 12;
    teacher.Name = "Jones";
    teacher.Qualifications.QualificationID = 7;
    teacher.Dump(); 
}

这类似于循环中的部分。问题是teacher.Qualifications需要一个对象的实例。但你没有创造一个。在您的第teacher.Qualifications.QualificationID行中,您尝试为非静态property QualificationID分配值。

你可以写

var qual = new Qualifications();
qual.QualificationID = 7;
teacher.Qualifications = qual;

如果您需要更多帮助,请与我们联系。