Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a full working example similar to the Getting Started guide <a href="https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started" rel="nofollow noreferrer">https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started</a></p> <pre><code> //=====CONSOLE MAIN using System; using System.Collections.Generic; using System.Linq; using System.Text; using FluentNHibernate.Cfg; using FluentNHibernate.Cfg.Db; using NHibernate; using NHibernate.Cfg; using NHibernate.Tool.hbm2ddl; using System.IO; using FluentNHibernate.Automapping; using App4.Entities; using System.Diagnostics; namespace App4 { class Program { static void Main(string[] args) { // create our NHibernate session factory var sessionFactory = CreateSessionFactory(); using (var session = sessionFactory.OpenSession()) { // populate the database using (var transaction = session.BeginTransaction()) { // create a couple of Stores each with some Products and Employees var topShelf = new Shelf(); var sw = new Stopwatch(); sw.Start(); for (var i = 0; i &lt; 1000; i++) { var potatoes = new Product { Name = "Potatoes" + i.ToString(), Price = 3.60 + i }; var meat = new Product { Name = "Meat" + i.ToString(), Price = 4.49 + i }; //session.SaveOrUpdate(potatoes); //===&lt;&lt;cascading save handles this :-) //session.SaveOrUpdate(meat); topShelf.Products.Add(meat); topShelf.Products.Add(potatoes); } sw.Stop(); session.SaveOrUpdate(topShelf); //session.SaveOrUpdate(superMart); transaction.Commit(); Console.WriteLine("Add Items: " + sw.ElapsedMilliseconds); } } using (var session = sessionFactory.OpenSession()) { // retreive all stores and display them using (session.BeginTransaction()) { var shelves = session.CreateCriteria(typeof(Shelf)).List&lt;Shelf&gt;(); foreach (var store in shelves) { WriteShelfPretty(store); } } } Console.ReadLine(); } private const string DbFile = "FIVEProgram.db"; private static ISessionFactory CreateSessionFactory() { return Fluently.Configure() .Database(SQLiteConfiguration.Standard.UsingFile(DbFile)) .Mappings(m =&gt; m.AutoMappings .Add(AutoMap.AssemblyOf&lt;Shelf&gt;(type =&gt; type.Namespace.EndsWith("Entities")) .Override&lt;Shelf&gt;(map =&gt; { map.HasManyToMany(x =&gt; x.Products);//.Cascade.All(); }) .Conventions.AddFromAssemblyOf&lt;CascadeAll&gt;() ) ) //emd mappings .ExposeConfiguration(BuildSchema)//Delete and remake db (see function below) .BuildSessionFactory();//finalizes the whole thing to send back. } private static void BuildSchema(Configuration config) { // delete the existing db on each run if (File.Exists(DbFile)) File.Delete(DbFile); // this NHibernate tool takes a configuration (with mapping info in) // and exports a database schema from it new SchemaExport(config) .Create(false, true); } private static void WriteShelfPretty(Shelf shelf) { Console.WriteLine(shelf.Id); Console.WriteLine(" Products:"); foreach (var product in shelf.Products) { Console.WriteLine(" " + product.Name); } Console.WriteLine(); } } } //Data Classes using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace App4.Entities { public class Product { public virtual int Id { get; private set; } public virtual string Name { get; set; } public virtual double Price { get; set; } } public class Shelf { public virtual int Id { get; private set; } public virtual IList&lt;Product&gt; Products { get; private set; } public Shelf() { Products = new List&lt;Product&gt;(); } } } //Cascade All Helper Class using FluentNHibernate.Conventions; using FluentNHibernate.Conventions.AcceptanceCriteria; using FluentNHibernate.Conventions.Inspections; using FluentNHibernate.Conventions.Instances; using System; using System.Collections.Generic; namespace App4 { public class CascadeAll : IHasOneConvention, //Actually Apply the convention IHasManyConvention, IReferenceConvention, IHasManyToManyConvention, IHasOneConventionAcceptance, //Test to see if we should use the convention IHasManyConventionAcceptance, //I think we could skip these since it will always be true IReferenceConventionAcceptance, //adding them for reference later IHasManyToManyConventionAcceptance { //One to One public void Accept(IAcceptanceCriteria&lt;IOneToOneInspector&gt; criteria) { //criteria.Expect(x =&gt; (true)); } public void Apply(IOneToOneInstance instance) { instance.Cascade.All(); } //One to Many public void Accept(IAcceptanceCriteria&lt;IOneToManyCollectionInspector&gt; criteria) { //criteria.Expect(x =&gt; (true)); } public void Apply(IOneToManyCollectionInstance instance) { instance.Cascade.All(); } //Many to One public void Accept(IAcceptanceCriteria&lt;IManyToOneInspector&gt; criteria) { // criteria.Expect(x =&gt; (true)); } public void Apply(IManyToOneInstance instance) { instance.Cascade.All(); } //Many to Many public void Accept(IAcceptanceCriteria&lt;IManyToManyCollectionInspector&gt; criteria) { // criteria.Expect(x =&gt; (true)); } public void Apply(IManyToManyCollectionInstance instance) { instance.Cascade.All(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload