mvc3에서 nhibernate를 사용하여 Question-Answer Forum이라는 응용 프로그램을 만듭니다. 첫 번째 페이지에는 링크 목록으로 표시된 질문 목록이 하나 있는데, 클릭 할 때 직접 클릭합니다 나 대답 페이지로.mvc3 nihibernate 외래 키에 값을 삽입하려면
테이블 구조 :
질문 표 :
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';
}
}
01 23,516,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';
}
}
내 매핑 파일 : Answer_Page 클래스에 데이터를 저장하는 동안 난 내 컨트롤러
이제 Answer_page 클래스에서 외부 키로 Question_page 클래스에서 ID를 만든
<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
에 대한 :"개체가 초기화되지 않았습니다. 오류가 발생합니다. nce " 정적 값을 ans.Question.Id = que.Id에 전달하더라도; 여전히 동일한 오류가 발생합니다. 내 외국 키에 값을 할당하는 방법을 알려주십시오.
두 개의 매핑 파일을 게시 할 수 있습니까? 네가 저기에있는 것처럼 보이지만 어쩌면 깜빡 했니? :) – mattytommo
@mattytommo 나는 두 파일을 올려주세요 제발 봐 – user1274646