结合 Fluent 和 XML 映射以实现 NHibnernate

发布于 2024-07-07 20:06:53 字数 2017 浏览 11 评论 0原文

我刚刚爱上了 NHibernate 和流畅的界面。 后者通过重构支持实现非常好的映射(不再需要 xml 文件)。

但没有人是完美的,所以我错过了流利的多对多映射。 有人知道它是否已经存在吗? 如果是这样,一行简单的代码就很好了。

但要坚持问题的标题,是否有任何方法可以将流畅的 NHibernate 映射与普通的 NHibernate 映射结合起来。

目前,我使用以下几行进行 fluid 测试设置,并使用第二个代码块进行不 fluid 测试(使用 XML 映射)。 我怎样才能告诉流利使用流利 IF AVAILABLE 和 XML 否则...

        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();

没有流利...

        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();

就是这样... Chris

PS:我真的很喜欢这个网站,GUI 非常完美,所有文章的质量都令人难以置信。 我认为这将是巨大的:-)必须注册......

I just fell in love with NHibernate and the fluent interface. The latter enables very nice mappings with refactoring support (no more need for xml files).

But nobody is perfect, so I am missing the many-to-any mapping in fluent. Does anybody know if it is already there? If so, a simple line of code would be nice.

But to stick to the header of the question, is there any way to combine fluent and normal NHibernate mapping.

Currently I use the following lines for my test setup WITH fluent, and the second code block for my test WITHOUT fluent (with XML mappings). How can I tell fluent to use fluent 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 Fluent...

        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();

That's it...
Chris

PS: I really like this site, the GUI is perfect, and the quality of all articles is incredible. I think it will be huge :-) Have to register...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风为裳 2024-07-14 20:06:54

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 位于最后。 我假设您的第一个示例实际上正在加载流畅的映射,但此时它已经创建了架构。

ManyToAny's currently aren't implemented (as of time of writing).

Regarding your setup for fluent and non-fluent mappings, you're almost there with your first example.

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);

The main difference is that the SchemaExport is last. I assume your first example was actually loading the fluent mappings, but it'd already created the schema by that point.

变身佩奇 2024-07-14 20:06:54

您可以完全在 Fluent NHibernate 中完成您想做的事情。

以下代码将使用 Fluent NHibernate 语法来流畅地配置会话工厂,该工厂从多个可能的程序集中查找 HBM (xml) 映射文件、流畅映射和约定。

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();

还有许多其他选项可供您使用:Fluent NHibernate 数据库配置

You can do exactly what you want to do entirely within Fluent NHibernate.

The following code will use Fluent NHibernate syntax to fluently configure a session factory that looks for HBM (xml) mapping files, fluent mappings, and conventions from multiple 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 as well: Fluent NHibernate Database Configuration

铜锣湾横着走 2024-07-14 20:06:54

Foo 映射到 Baa

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

查看 ClassMapXmlCreationTester - 它们还显示默认列名称是什么。

Mapping from Foo to Baa:

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

Check out the examples in ClassMapXmlCreationTester - they also show what the default column names are.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文