繁体   English   中英

在mvc3 nihibernate的外键中插入值

[英]To insert a value in foreign key in mvc3 nihibernate

我在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,我也会在“对象未实例化到对象”中得到错误提示。 我仍然遇到相同的错误,请告诉我如何为外键分配值

我认为我已经看到问题了,您已经在Answer映射文件中将问题与Answer链接了,但是您没有将QuestionId放在Answer映射文件或Answer类中。

更改此行:

ans.Question.Id = que.Id;

到(将QuestionId添加到Answer映射和类中之后):

ans.QuestionId = que.Id;

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

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

希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM