要插入在MVC3 nihibernate外键的值 [英] To insert a value in foreign key in mvc3 nihibernate

查看:118
本文介绍了要插入在MVC3 nihibernate外键的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在制作MVC3使用NHibernate的应用程序命名的问答论坛
其中第一页我有显示为链接问题的列表,当我上单击一下它引导我的答案页面。

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.

表结构:

问题表:

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)

这是Model类:

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';
}
}

和我的映射文件:
对于Question_page

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>

我已经在Answer_page类制成的ID从Question_page类外键

i have made id from Question_page class as foreign key in Answer_page class

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

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());
    }

我得到一个错误在说不反对向intialised实例
即使我通过一个静态值ans.Question.Id = que.Id;
我仍然得到同样的错误
请告诉我,我应该如何将值分配给我的外键

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

推荐答案

我觉得我看到了问题,你就挂在应答映射文件回答这个问题,但你有没有把QuestionId无论是​​在回答映射文件或应答类。

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.

更改此行:

ans.Question.Id = que.Id;

要(您添加QuestionId后进入应答映射和放大器;类):

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

ans.QuestionId = que.Id;

你收到这个错误的原因是因为你已经在正确映射链接的问题对象,但你不能提供一个字段的NHibernate的可以找到问题的对象,因此它是一个空的对象。

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.

尝试保存你的新QuestionId答​​案填充,然后加载它在你的应用程序,并尝试把手表问题的对象,你应该看到它满载。

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.

希望有所帮助。

这篇关于要插入在MVC3 nihibernate外键的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆