在mvc3 nhibernate中的外键中插入一个值

时间:2012-03-20 17:39:20

标签: asp.net-mvc asp.net-mvc-3 nhibernate nhibernate-mapping nhibernate-criteria

我正在使用mvc3中的nhibernate创建一个名为Question-Answer Forum的应用程序 在第一页我有一个显示为链接的问题列表,当我点击它时,它会将我引导到答案页面。

表格结构:

问题表:

QuestionID int
Question nvarchar(255)
Created_Date datetime
Modified_Date datetime
Created_By int
Modified_By int
Deleted nchar(1)

答案表:

Id int
Answer nvarchar(255)
Created_Date datetime 
Modified_Date datetime  
Created_By int 
Modified_By  int
QuestionID int 
Deleted  nchar(1)

这些是模型类:

public class Question_Page
{
 public virtual int Id { get; set; }
 public virtual string Question_name { get; set; }
 public virtual DateTime Created_Date { get; set; }
 public virtual DateTime Modified_Date { get; set; }
 public virtual int Created_By { get; set; }
 public virtual int Modified_By { get; set; }
 public virtual char Deleted { get; set; }

 public Question_Page()
{
    this.Deleted='F';
}

}

public class Answer_Page
{
public virtual int Id { get; set; }
public virtual string Answer { get; set; }
public virtual DateTime Created_Date { get; set; }
public virtual DateTime Modified_Date { get; set; }
public virtual int Created_By { get; set; }
public virtual int Modified_By { get; set; }
public virtual char Deleted { get; set; }
public virtual Question_Page Question { get; set; }
public virtual int QuestionID{get;set;}
public Answer_Page()
{
    this.Deleted='F';
}
}

和我的映射文件: 对于Question_page

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly ="Core" namespace ="Core.Model" >
<class name ="Question_Page" >
<id name="Id">
<generator class="native" />
</id>
<property name="Question_name"    />
<property name="Created_Date"  />
<property name="Modified_Date"  />
<property name="Created_By"  />
<property name="Modified_By"  />
<property name="Deleted"/>
</class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly ="Core" namespace ="Core.Model" >
<class name ="Answer_Page" >
<id name="Id">
<generator class="native" />
</id>
<property name="Answer"    />
<property name="Created_Date"  />
<property name="Modified_Date"  />
<property name="Created_By"  />
<property name="Modified_By"  />
<property name="Deleted"/>
<property name="QuestionID"/>
<many-to-one class="Core.Model.Question_Page" name="Question" column="QuestionID" foreign- key="fk_Question_Answer" />
</class>
</hibernate-mapping>

我已将Question_page类的id作为Answer_page类中的外键

现在在我的控制器中保存Answer_Page类中的数据:

 public ActionResult Answer(Answer_Page ans, string PostyourAnswer,Question_Page que)
    {
        ans.Answer = PostyourAnswer;
        ans.Created_Date = DateTime.Now;
        ans.Modified_Date = DateTime.Now;
        ans.Created_By = 101;
        ans.Modified_By = 101;
        ans.QuestionID = que.Id;
        new Answer().SaveOrUpdateStudent(ans);
        return View(new Answer().GetAllStudents());
    }

我收到错误说“对象未初始化为实例” 即使我将静态值传递给ans.Question.Id = que.Id; 我仍然得到同样的错误 请告诉我如何为外键指定值

1 个答案:

答案 0 :(得分:0)

我想我看到了问题,你已经在答案映射文件中链接了问题与答案,但你没有将QuestionId放在答案映射文件或答案类中。

更改此行:

ans.Question.Id = que.Id;

to(在你将AnswerId添加到Answer map&amp; class中之后):

ans.QuestionId = que.Id;

您收到该错误的原因是因为您已在映射中正确链接了Question对象,但您没有提供NHibernate可以找到Question对象的列,因此它是一个null对象。

尝试使用填充的新QuestionId保存答案,然后将其加载到应用程序中并尝试监视Question对象,您应该看到它已完全加载。

希望有所帮助。