To insert a value in foreign key in mvc3 nihiberna

2019-09-15 16:45发布

问题:

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

回答1:

I think I see the problem, you've linked the Question with Answer in the Answer mapping file, but you haven't put the QuestionId in either the Answer mapping file or the Answer class.

Change this line:

ans.Question.Id = que.Id;

to (after you've added QuestionId into the Answer mapping & class):

ans.QuestionId = que.Id;

The reason you were getting that error is because you've linked the Question object correctly in the mapping, but you've not provided a column in which NHibernate can find the Question object for, therefore it's a null object.

Try saving an answer with your new QuestionId populated, then load it in your application and try putting a watch on the Question object, you should see it fully loaded.

Hope that helps.