Created
June 8, 2011 13:14
-
-
Save Buthrakaur/1014394 to your computer and use it in GitHub Desktop.
NH one-to-one
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using FluentNHibernate.Cfg; | |
using FluentNHibernate.Cfg.Db; | |
using FluentNHibernate.Mapping; | |
using NHibernate; | |
using NHibernate.Cfg; | |
using NHibernate.Tool.hbm2ddl; | |
using Xunit; | |
namespace NHListenerTest | |
{ | |
public class Projekt | |
{ | |
public virtual int Id { get; set; } | |
public virtual string Name { get; set; } | |
public virtual Rozpocet Rozpocet { get; set; } | |
} | |
public class Rozpocet | |
{ | |
public virtual int Id { get; set; } | |
public virtual string Name { get; set; } | |
public virtual Projekt Projekt { get; set; } | |
} | |
public class ProjektMap: ClassMap<Projekt> | |
{ | |
public ProjektMap() | |
{ | |
Id(x => x.Id).GeneratedBy.Assigned(); | |
Map(x => x.Name); | |
HasOne(x => x.Rozpocet); | |
} | |
} | |
public class RozpocetMap: ClassMap<Rozpocet> | |
{ | |
public RozpocetMap() | |
{ | |
Id(x => x.Id).GeneratedBy.Assigned(); | |
Map(x => x.Name); | |
HasOne(x => x.Projekt); | |
} | |
} | |
public class BuilderNHTest: IDisposable | |
{ | |
private readonly ISessionFactory sessionFactory; | |
private readonly ISession session; | |
public BuilderNHTest() | |
{ | |
Configuration config = null; | |
sessionFactory = Fluently.Configure() | |
.Database(SQLiteConfiguration.Standard.InMemory) | |
.Mappings(m => | |
{ | |
m.FluentMappings.Add<RozpocetMap>(); | |
m.FluentMappings.Add<ProjektMap>(); | |
}) | |
.ExposeConfiguration(cfg => | |
{ | |
config = cfg; | |
cfg.SetProperty("show_sql", "true"); | |
}) | |
.BuildSessionFactory(); | |
session = sessionFactory.OpenSession(); | |
using (var tx = session.BeginTransaction()) | |
{ | |
new SchemaExport(config).Execute(false, true, false, session.Connection, null); | |
tx.Commit(); | |
} | |
session.BeginTransaction(); | |
} | |
public void Dispose() | |
{ | |
session.Transaction.Rollback(); | |
session.Dispose(); | |
sessionFactory.Dispose(); | |
} | |
[Fact] | |
public void CanPersist() | |
{ | |
var p = new Projekt | |
{ | |
Id = 1, | |
Name = "p1" | |
}; | |
var r = new Rozpocet | |
{ | |
Id = 1, | |
Name = "r1" | |
}; | |
session.Save(p); | |
session.Save(r); | |
session.Flush(); | |
session.Clear(); | |
var xP = session.Load<Projekt>(1); | |
var xR = session.Load<Rozpocet>(1); | |
Assert.Same(xP, xR.Projekt); | |
Assert.Same(xR, xP.Rozpocet); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment