I am making an application named Question-Answer Forum using nhibernate in mvc3 One the first page i have a list of questions displayed as link and when i click on the click it directs me to the answers page.
Table Structure:
Questions Table:
QuestionID int
Question nvarchar(255)
Created_Date datetime
Modified_Date datetime
Created_By int
Modified_By int
Deleted nchar(1)
Answers Table:
Id int
Answer nvarchar(255)
Created_Date datetime
Modified_Date datetime
Created_By int
Modified_By int
QuestionID int
Deleted nchar(1)
These are Model classes:
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';
}
}
and my mapping files: for 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>
i have made id from Question_page class as foreign key in Answer_page class
Now in my controller while saving data in Answer_Page class:
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());
}
I get an error sayin "Object not intialised to the instance" even if i pass a static value to ans.Question.Id = que.Id; still i get the same error please tell me how should i assign value to my foreign key