我是NHibernate的新手。我在映射文件中遇到问题。我有2个表Fetures和Priority。 功能ID(PK),功能名称,优先级ID(FK) Priorty PriorityID(PK),PriorityName
我想将网格绑定到Feature表,但网格应包含PriorityName而不是PriorityID。 我尝试了一对一,多对一和包。 请帮助我如何编写映射文件,以便我可以在Feature类中获得特定PriorityID的PriorityName。
我知道这是一个非常简单的问题。但对我来说没有任何作用。经过大量的谷歌搜索后,我在这里发帖。
请帮帮我 在此先感谢
答案 0 :(得分:1)
以下是使用Fluent NHibernate和SQLite的示例:
using System.Data;
using System.IO;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Mapping;
using NHibernate;
public class Priority
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class Feature
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Priority Priority { get; set; }
}
public class PriorityMap : ClassMap<Priority>
{
public PriorityMap()
{
WithTable("Priority");
Id(x => x.Id, "PriorityID");
Map(x => x.Name, "PriorityName");
}
}
public class FeatureMap : ClassMap<Feature>
{
public FeatureMap()
{
WithTable("Feature");
Id(x => x.Id, "FeatureID");
Map(x => x.Name, "FeatureName");
References<Priority>(x => x.Priority, "PriorityID");
}
}
public static class SessionFactoryEx
{
private const string _dbFile = @"C:\data.db3";
static SessionFactoryEx()
{
if (File.Exists(_dbFile))
{
File.Delete(_dbFile);
}
using (var factory = SessionFactoryEx.GetSessionFactory())
using (var connection = factory.ConnectionProvider.GetConnection())
{
SessionFactoryEx.ExecuteQuery("create table Priority(PriorityID int, PriorityName string)", connection);
SessionFactoryEx.ExecuteQuery("create table Feature(FeatureID int, FeatureName string, PriorityID int)", connection);
SessionFactoryEx.ExecuteQuery("insert into Priority (PriorityID, PriorityName) values (1, 'p1')", connection);
SessionFactoryEx.ExecuteQuery("insert into Feature (FeatureID, FeatureName, PriorityID) values (1, 'f1', 1)", connection);
SessionFactoryEx.ExecuteQuery("insert into Feature (FeatureID, FeatureName, PriorityID) values (2, 'f2', 1)", connection);
SessionFactoryEx.ExecuteQuery("insert into Feature (FeatureID, FeatureName, PriorityID) values (3, 'f3', 1)", connection);
}
}
private static ISessionFactory _sessionFactory = null;
public static ISessionFactory GetSessionFactory()
{
if (_sessionFactory == null)
{
_sessionFactory = CreateSessionFactory();
}
return _sessionFactory;
}
private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(
SQLiteConfiguration.Standard.UsingFile(_dbFile).ShowSql()
)
.Mappings(
m => m.FluentMappings.AddFromAssemblyOf<Priority>()
).BuildSessionFactory();
}
public static void ExecuteQuery(string sql, IDbConnection connection)
{
using (var command = connection.CreateCommand())
{
command.CommandText = sql;
command.ExecuteNonQuery();
}
}
}
在ASPX页面中,您可以绑定数据:
<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (var factory = SessionFactoryEx.GetSessionFactory())
using (var session = factory.OpenSession())
using (var tx = session.BeginTransaction())
{
var features = session.CreateCriteria(typeof(Feature)).List<Feature>();
featuresGrid.DataSource = features;
featuresGrid.DataBind();
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="featuresGrid" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<%# Eval("Id") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Priority Name">
<ItemTemplate>
<%# Eval("Priority.Name") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>