首先建立两张表
一张项目表,另外一张项目文件表。
项目表: 项目文件表:
项目文件中的F_PId为外键。
下面生成映射文件,使用工具mygeneration自动生成。如下是:
项目文件映射XML
<class name="" table="T_Project_File" lazy="true">
<id name="Id" column="Id" type="string">
<generator class="assigned" />
</id>
<property type="int" not-null="true" name="FType" column="[F_Type]" />
<property type="string" length="100" name="FClientName" column="[F_ClientName]" />
<property type="string" length="100" name="FServerName" column="[F_ServerName]" />
<property type="string" length="100" name="FExpandName" column="[F_ExpandName]" />
<property type="string" length="100" name="FUrl" column="[F_Url]" />
<property type="DateTime" name="FTime" column="[F_Time]" />
<property type="string" length="100" name="FPerson" column="[F_Person]" />
<many-to-one name="Tproject" cascade="none" column="F_PId" not-null="true" />
</class>
<id name="Id" column="Id" type="string">
<generator class="assigned" />
</id>
<property type="int" not-null="true" name="FType" column="[F_Type]" />
<property type="string" length="100" name="FClientName" column="[F_ClientName]" />
<property type="string" length="100" name="FServerName" column="[F_ServerName]" />
<property type="string" length="100" name="FExpandName" column="[F_ExpandName]" />
<property type="string" length="100" name="FUrl" column="[F_Url]" />
<property type="DateTime" name="FTime" column="[F_Time]" />
<property type="string" length="100" name="FPerson" column="[F_Person]" />
<many-to-one name="Tproject" cascade="none" column="F_PId" not-null="true" />
</class>
下面是一个实例:
当用户想通过输入项目名称来查询是否有项目文件时,该怎么办呢,这个时候可以在项目文件中使用多对一这种方法,当然肯定会有很多种办法的。
代码如下:
protected IQueryOver<TProjectFile, TProjectFile> getProjectFileList()
{
string projectName = txtProjectName.Text.Trim();
if (UtilityClass.IsNullOrEmpty(projectName))
{
IQueryOver<TProjectFile, TProjectFile> projectFileQuery = session.QueryOver<TProjectFile>();
return projectFileQuery;
}
else
{
IQueryOver<TProjectFile, TProjectFile> projectFileQuery = session
.QueryOver<TProjectFile>();
projectFileQuery.JoinQueryOver(pf => pf.Tproject).Where(p => p.PName.IsLike(projectName, MatchMode.Anywhere));
return projectFileQuery;
}
}
{
string projectName = txtProjectName.Text.Trim();
if (UtilityClass.IsNullOrEmpty(projectName))
{
IQueryOver<TProjectFile, TProjectFile> projectFileQuery = session.QueryOver<TProjectFile>();
return projectFileQuery;
}
else
{
IQueryOver<TProjectFile, TProjectFile> projectFileQuery = session
.QueryOver<TProjectFile>();
projectFileQuery.JoinQueryOver(pf => pf.Tproject).Where(p => p.PName.IsLike(projectName, MatchMode.Anywhere));
return projectFileQuery;
}
}
下载映射文件/Files/ylwn817/TprojectFile.rar