Skip to content

Instantly share code, notes, and snippets.

@maxfridbe
Created November 15, 2012 19:15
Show Gist options
  • Save maxfridbe/4080591 to your computer and use it in GitHub Desktop.
Save maxfridbe/4080591 to your computer and use it in GitHub Desktop.
postgress npgsql
class Program
{
static void Main(string[] args)
{
var sqlConfig = MsSqlConfiguration.MsSql2008.ConnectionString(@"Server=.\SQLEXPRESS;Database=testdb3;Trusted_Connection=True;");
var config = PostgreSQLConfiguration.PostgreSQL82.ConnectionString(c =>
c.Database("testdb3")
.Host("localhost")
.Port(5432)
.Username("all")
.Password("all") );
var sessionFactory = Fluently.Configure()
.Database(sqlConfig)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
.ExposeConfiguration(cfg => { new SchemaUpdate(cfg).Execute(false, true); })
.BuildSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
for (int j = 0; j < 100; j++)
{
var r = new Residence() {Address = "Residence #" + j, Id = Guid.NewGuid()};
session.Save(r);
for (int i = 0; i < 10; i++)
{
var p = new Person() {Id = Guid.NewGuid(), Name = "PersonName #" + (i + j)};
p.Residence = r;
session.Save(p);
}
}
transaction.Commit();
}
}
using (var session = sessionFactory.OpenSession())
{
var result = (from res in session.Query<Residence>()
where res.Address == "Residence #50"
select res).FirstOrDefault();
foreach (var inhabitant in result.Inhabitants)
{
Console.WriteLine(inhabitant.Name);
}
}
}
}
public class Residence
{
public Residence()
{
Inhabitants = new List<Person>();
}
public virtual Guid Id { get; set; }
public virtual string Address { get; set; }
public virtual IList<Person> Inhabitants { get; set; }
}
public class Person
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual Residence Residence { get; set; }
}
public class PersonMap:ClassMap<Person>
{
public PersonMap()
{
this.Id(x => x.Id);
this.Map(x => x.Name);
this.References(x => x.Residence).Column("ResidenceId");
}
}
public class ResidenceMap : ClassMap<Residence>
{
public ResidenceMap()
{
this.Id(x => x.Id);
this.Map(x => x.Address);
this.HasMany(x => x.Inhabitants).KeyColumn("ResidenceId");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment