Skip to content

Instantly share code, notes, and snippets.

@oguzhaneren
Created May 26, 2014 08:42
Show Gist options
  • Save oguzhaneren/bbbcc4f8dbef5dc6f2f4 to your computer and use it in GitHub Desktop.
Save oguzhaneren/bbbcc4f8dbef5dc6f2f4 to your computer and use it in GitHub Desktop.
Fluent NHibernate AutoMapping
static void TreatConfiguration(NHibernate.Cfg.Configuration configuration)
{
var update = new SchemaUpdate(configuration);
update.Execute(false, true);
}
private void Configure()
{
var mappings = AutoMap.AssemblyOf<Person>()
.Where(x => x == typeof (Person))
.Conventions.Add
(
ConventionBuilder.Id.Always(x=>x.GeneratedBy.GuidComb()),
ConventionBuilder.HasMany.Always(x => x.Cascade.AllDeleteOrphan()),
//Table.Is(o => Inflector.Pluralize(o.EntityType.Name)),
PrimaryKey.Name.Is(o => "Id"),
ForeignKey.EndsWith("Id"),
DefaultCascade.All()
);
var sessionFactory = Fluently.Configure().Database(MsSqlConfiguration.MsSql2008.ConnectionString("server=localhost;database=nh_selecttree;user=sa;password=sql1234"))
.Mappings(val => val.AutoMappings.Add(mappings))
.ExposeConfiguration(TreatConfiguration)
.BuildSessionFactory();
return sessionFactory;
}
public class Person
{
private readonly Iesi.Collections.Generic.ISet<Person> _persons = new Iesi.Collections.Generic.HashedSet<Person>();
public virtual IEnumerable<Person> Persons
{
get { return _persons; }
}
public virtual Guid ID { get; set; }
public virtual string Name { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment