Created
April 13, 2013 08:01
-
-
Save anonymous/5377535 to your computer and use it in GitHub Desktop.
When defining order-by in collection mappings, NHibernate does not qualify order-by with table name if Sql Server is used. It does work with Sql Server CE, though.
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
// uncomment this to use SQL Server instead | |
#define USE_SQL_SERVER_CE | |
namespace AmbiguousColumn | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Data.SqlClient; | |
using System.Data.SqlServerCe; | |
using System.IO; | |
using NHibernate.Cfg; | |
using NHibernate.Cfg.MappingSchema; | |
using NHibernate.Connection; | |
using NHibernate.Dialect; | |
using NHibernate.Driver; | |
using NHibernate.Mapping.ByCode; | |
using NHibernate.Tool.hbm2ddl; | |
public class Node | |
{ | |
public Node() | |
{ | |
Children = new List<Node>(); | |
} | |
public virtual int Id { get; set; } | |
public virtual IList<Node> Children { get; protected set; } | |
public virtual Node Parent { get; protected set; } | |
public virtual int Position | |
{ | |
get { return Parent == null ? -1 : Parent.Children.IndexOf(this); } | |
protected set { } | |
} | |
public virtual void AddChild(Node child) | |
{ | |
child.Parent = this; | |
Children.Add(child); | |
} | |
} | |
public class Program | |
{ | |
public static void Main() | |
{ | |
var compiledMappings = MapModel(); | |
var config = ConfigureNHibernate(compiledMappings); | |
using (var sf = config.BuildSessionFactory()) | |
using (var session = sf.OpenSession()) | |
using (var txn = session.BeginTransaction()) | |
{ | |
var children = session.QueryOver<Node>() | |
.Fetch(x => x.Children).Eager | |
.List(); | |
txn.Commit(); | |
} | |
} | |
private static Configuration ConfigureNHibernate(HbmMapping compiledMappings) | |
{ | |
var config = new Configuration(); | |
#if USE_SQL_SERVER_CE | |
ConfigureSqlServerCE(config); | |
#else | |
ConfigureSqlServerExpress(config); | |
#endif | |
config.AddMapping(compiledMappings); | |
new SchemaExport(config).Execute(true, true, false); | |
return config; | |
} | |
private static HbmMapping MapModel() | |
{ | |
var modelMapper = new ModelMapper(); | |
modelMapper.Class<Node>(cls => | |
{ | |
cls.Id(x => x.Id, map => map.Generator(Generators.Identity)); | |
cls.ManyToOne(x => x.Parent, map => map.NotNullable(false)); | |
cls.Property(x => x.Position); | |
cls.Bag(x => x.Children, | |
map => | |
{ | |
map.Inverse(true); | |
map.Cascade(Cascade.All); | |
map.Key(key => key.Column("Parent")); | |
map.OrderBy(child => child.Position); | |
}, | |
map => map.OneToMany()); | |
}); | |
var compiledMappings = modelMapper.CompileMappingForAllExplicitlyAddedEntities(); | |
Console.WriteLine(compiledMappings.AsString()); | |
return compiledMappings; | |
} | |
private static void ConfigureSqlServerExpress(Configuration config) | |
{ | |
config.DataBaseIntegration(db => | |
{ | |
db.Dialect<MsSql2012Dialect>(); | |
db.Driver<Sql2008ClientDriver>(); | |
db.LogFormattedSql = true; | |
db.LogSqlInConsole = true; | |
db.ConnectionProvider<DriverConnectionProvider>(); | |
db.ConnectionString = new SqlConnectionStringBuilder | |
{ | |
DataSource = @".\SQLEXPRESS", | |
InitialCatalog = "AmbiguousColumn", | |
IntegratedSecurity = true | |
}.ConnectionString; | |
}); | |
} | |
private static void ConfigureSqlServerCE(Configuration config) | |
{ | |
if (File.Exists("data.db")) | |
{ | |
File.Delete("data.db"); | |
} | |
File.Create("data.db").Dispose(); | |
config.DataBaseIntegration(db => | |
{ | |
db.Dialect<MsSqlCe40Dialect>(); | |
db.Driver<SqlServerCeDriver>(); | |
db.LogFormattedSql = true; | |
db.LogSqlInConsole = true; | |
db.ConnectionProvider<DriverConnectionProvider>(); | |
db.ConnectionString = new SqlCeConnectionStringBuilder { DataSource = "data.db" }.ConnectionString; | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment