I have following code:
//AutoMapConfig.cs
using System;
using FluentNHibernate.Automapping;
namespace SimpleFNH.AutoMap
{
public class AutoMapConfig : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Type type)
{
return type.Namespace == "Examples.FirstAutomappedProject.Entities";
}
}
}
//CascadeConvention.cs
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;
namespace SimpleFNH.AutoMap
{
public class CascadeConvention : IReferenceConvention, IHasManyConvention, IHasManyToManyConvention
{
public void Apply(IManyToOneInstance instance)
{
instance.Cascade.All();
}
public void Apply(IOneToManyCollectionInstance instance)
{
instance.Cascade.All();
}
public void Apply(IManyToManyCollectionInstance instance)
{
instance.Cascade.All();
}
}
}
//Item.cs
namespace SimpleFNH.Entities
{
public class Item
{
public virtual long ID { get; set; }
public virtual string ItemName { get; set; }
public virtual string Description { get; set; }
public virtual OrderItem OrderItem { get; set; }
}
}
//OrderItem.cs
namespace SimpleFNH.Entities
{
public class OrderItem
{
public virtual long ID { get; set; }
public virtual int Quantity { get; set; }
public virtual Item Item { get; set; }
public virtual ProductOrder ProductOrder { get; set; }
public virtual void AddItem(Item item)
{
item.OrderItem = this;
}
}
}
using System;
using System.Collections.Generic;
//ProductOrder.cs
namespace SimpleFNH.Entities
{
public class ProductOrder
{
public virtual long ID { get; set; }
public virtual DateTime OrderDate { get; set; }
public virtual string CustomerName { get; set; }
public virtual IList<OrderItem> OrderItems { get; set; }
public ProductOrder()
{
OrderItems = new List<OrderItem>();
}
public virtual void AddOrderItems(params OrderItem[] items)
{
foreach (var item in items)
{
OrderItems.Add(item);
item.ProductOrder = this;
}
}
}
}
//NHibernateRepo.cs
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Tool.hbm2ddl;
namespace SimpleFNH.Repository
{
public class NHibernateRepo
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
InitializeSessionFactory();
return _sessionFactory;
}
}
private static void InitializeSessionFactory()
{
_sessionFactory =
Fluently.Configure().Database(
MsSqlConfiguration.MsSql2008.ConnectionString(
@"server=Amit-PC\SQLEXPRESS;database=SimpleFNH;Trusted_Connection=True;").ShowSql()).
Mappings(m => m.FluentMappings.AddFromAssemblyOf<Order>()).ExposeConfiguration(
cfg => new SchemaExport(cfg).Create(true, true)).BuildSessionFactory();
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
}
//Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using SimpleFNH.Entities;
using SimpleFNH.Repository;
namespace SimpleFNH
{
class Program
{
static void Main(string[] args)
{
using (var session = NHibernateRepo.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var item1 = new Item { ItemName = "item 1", Description = "test 1" };
var item2 = new Item { ItemName = "item 2", Description = "test 2" };
var item3 = new Item { ItemName = "item 3", Description = "test 3" };
var orderItem1 = new OrderItem { Item = item1, Quantity = 2 };
var orderItem2 = new OrderItem { Item = item2, Quantity = 4 };
var orderItem3 = new OrderItem { Item = item3, Quantity = 5 };
var productOrder = new ProductOrder
{
CustomerName = "Amit",
OrderDate = DateTime.Now,
OrderItems = new List<OrderItem> { orderItem1, orderItem2, orderItem3 }
};
productOrder.AddOrderItems(orderItem1, orderItem2, orderItem3);
session.Save(productOrder);
transaction.Commit();
}
}
using (var session = NHibernateRepo.OpenSession())
{
// retreive all stores and display them
using (session.BeginTransaction())
{
var orders = session.CreateCriteria(typeof(ProductOrder))
.List<ProductOrder>();
foreach (var item in orders)
{
Console.WriteLine(item.OrderItems.First().Quantity);
}
}
}
}
}
}
I tried many variations to get it working but i get an error saying No persister for: SimpleFNH.Entities.ProductOrder
Can someone help me get it working? I wanted to create a simple program which will set a pattern for my bigger project but it is taking quite a lot of time than expected. It would be rally helpful if you can explain in simple terms on any template/pattern that i can use to get fluent nHibernate working.
The above code uses auto mapping, which i tried after i tried with fluent mapping.