Here's a sample using Fluent NHibernate and 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();
}
}
}
And in your ASPX page you can bind data:
<%@ 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>