views:

185

answers:

2

I am a newbie in NHibernate.I am facing problem in mapping file.I have 2 tables Fetures and Priority. Feature FeatureID(PK),FeatureName,PriorityID(FK) Priorty PriorityID(PK),PriorityName

I want to bind a grid to Feature table but but grid should contain PriorityName rather than PriorityID. I have tried one-to-one,many-to-one and bag. Please help me how can write mapping file so that I can get PriorityName for particular PriorityID in Feature class.

I know it is a very simple question.But nothing worked for me. After lots of googling I am posting here.

Please help me Thanks in Advance

+1  A: 

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"&gt;

<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"&gt;
<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>
Darin Dimitrov
A: 

Many thanks for reply

I have stated a project in Nhibernate using Nhibernate best practices http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx.I have two classes Feature and Priority. Is there any way that I can retrive the PriorityName (Property) in Feature class after writing some mapping element in Feature.hbm.xml

Please help me.

Regards, Raima