Skip to content

Instantly share code, notes, and snippets.

@chester89
Created April 24, 2013 21:00
Show Gist options
  • Save chester89/5455538 to your computer and use it in GitHub Desktop.
Save chester89/5455538 to your computer and use it in GitHub Desktop.
Fluent NHibernate mapping to a non-public field
public class TestClass
{
private decimal myField;
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual void SetField(decimal newOne)
{
myField = newOne;
}
public TestClass()
{
myField = 50m;
}
public TestClass(Decimal x)
{
myField = x;
}
}
public class MyMapping: ClassMap<TestClass>
{
public MyMapping()
{
Map(Reveal.Member<TestClass>("myField"));
Id(x => x.Id);
Map(x => x.Name);
}
}
public static void Main(params string[] args)
{
var config = Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2008.ShowSql().ConnectionString(
c => c.FromConnectionStringWithKey("db")))
.Mappings(x => x.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())).BuildConfiguration();
new SchemaExport(config).Create(true, true);
using(var factory = config.BuildSessionFactory())
{
using(var session = factory.OpenSession())
{
session.SaveOrUpdate(new TestClass(5.0m) { Name = "Some name" });
var fetched = session.Query<TestClass>().SingleOrDefault(x => x.Id == 1);
fetched.SetField(12m);
session.SaveOrUpdate(fetched);
session.Flush();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment