Combining Free and XML Mapping for NHibnernate

I just fell in love with NHibernate and the free interface. The latter allows for very nice mappings with refactoring support (XML files are no longer needed).

But no one is perfect, so I miss the multi-valued display on the run. Does anyone know if it already exists? If so, a simple line of code will be nice.

But stick to the title of the question, is there a way to combine free and normal NHibernate mapping.

I am currently using the following lines for my test setup for FREE, and the second code block for my test for FREE (with XML mapping). How can I talk freely about using the freely available IF AVAILABLE and XML otherwise ...

        var cfg = new Configuration();
        cfg.AddProperties(MsSqlConfiguration.MsSql2005.ConnectionString.Is(_testConnectionstring).ToProperties());
        cfg.AddMappingsFromAssembly(typeof(CatMap).Assembly);
        new SchemaExport(cfg).Create(true, true);

        var persistenceModel = new PersistenceModel();
        persistenceModel.addMappingsFromAssembly(typeof(CatMap).Assembly);
        IDictionary<string, string> properties = MsSqlConfiguration.MsSql2005.UseOuterJoin().ShowSql().ConnectionString.Is(_testConnectionstring).ToProperties();
        properties.Add("command_timeout", "340");

        session = new SessionSource(properties, persistenceModel).CreateSession();

Without spaces...

        config = new Configuration();
        IDictionary props = new Hashtable();

        props["connection.provider"] = "NHibernate.Connection.DriverConnectionProvider";
        props["dialect"] = "NHibernate.Dialect.MsSql2005Dialect";
        props["connection.driver_class"] = "NHibernate.Driver.SqlClientDriver";
        props["connection.connection_string"] = "Server=localhost;initial catalog=Debug;Integrated Security=SSPI";
        props["show_sql"] = "true";
        foreach (DictionaryEntry de in props)
        {
            config.SetProperty(de.Key.ToString(), de.Value.ToString());
        }
        config.AddAssembly(typeof(CatMap).Assembly);

        SchemaExport se = new SchemaExport(config);
        se.Create(true, true);

        factory = config.BuildSessionFactory();
        session = factory.OpenSession();

What is it ... Chris

PS: , , . , :-) ...

+5
3

Foo Baa:

HasManyToMany< Baa > ( x => Baas )
  .AsBag ( ) //can also be .AsSet()
  .WithTableName ( "foobar" )
  .WithParentKeyColumn ( "fooId" )
  .WithChildKeyColumn ( "barId" ) ;

ClassMapXmlCreationTester - , .

+2

ManyToAny ( ).

, .

var cfg = MsSqlConfiguration.MsSql2005
  .ConnectionString.Is(_testConnectionstring)
  .ConfigureProperties(new Configuration());

cfg.AddMappingsFromAssembly(typeof(CatMap).Assembly); // loads hbm.xml files

var model = new PersistenceModel();
model.addMappingsFromAssembly(typeof(CatMap).Assembly); // loads fluent mappings
mode.Configure(cfg);

new SchemaExport(cfg).Create(true, true);

, SchemaExport. , , .

+2

You can do exactly what you want to do completely in Fluent NHibernate.

The following code will use the Fluent NHibernate syntax to smoothly configure a factory session that looks for HBM mapping files (xml), smooth mapping, and legend from several possible assemblies.

var _mappingAssemblies = new Assembly[] { typeof(CatMap).Assembly };
var _autoPersistenceModel = CreateAutoPersistenceModel();
Fluently.Configure()
        .Database(MsSqlConfiguration.MsSql2005.ConnectionString(_testConnectionstring))
        .Mappings(m =>
                  {
                      foreach (var assembly in _mappingAssemblies)
                      {
                          m.HbmMappings.AddFromAssembly(assembly);
                          m.FluentMappings.AddFromAssembly(assembly)
                              .Conventions.AddAssembly(assembly);
                      }
                      m.AutoMappings.Add(_autoPersistenceModel );
                   })
        .ExposeConfiguration(c => c.SetProperty("command_timeout", "340"))
        .BuildSessionFactory();

There are many other options available to you: Free NHibernate Database Configuration

+2
source

All Articles