使用 Fluent NHibernate 使用 Nhibernate 生成数据库

发布于 2024-12-10 17:38:16 字数 1505 浏览 0 评论 0原文

我正在尝试使用(新的)Fluent NHibernate(尝试从 XML 映射文件切换到 FNH)。使用下面的代码,我生成了数据库,我试图找到相同的解决方案,但使用 FNH(我仍然想使用 hibernate.cfg.xml):

public void CreateDatabase()
{
    NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();

    cfg.Configure();
    SchemaMetadataUpdater.QuoteTableAndColumns(cfg);
    NHibernate.Tool.hbm2ddl.SchemaExport schema = new NHibernate.Tool.hbm2ddl.SchemaExport(cfg);

    schema.Create(false, true);
}


namespace MyTestApplication.Entities
{
    public class Person
    {
        public virtual int Id { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
    }
}

namespace MyTestApplication.Data.Mapping
{
    public class PersonMapping : ClassMap<Person>
    {
        public PersonMapping()
        {
            Id(x => x.Id);
            Map(x => x.FirstName);
            Map(x => x.LastName);      
        }
    }
}

解决方案

最后,我使用这个(感谢马可):

 public void CreationDB()
    {
        FluentConfiguration config = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008.ConnectionString("......"))
            .Mappings(
                m => m.FluentMappings.Add(typeof(MyTestApplication.Data.Mapping.PersonMapping)
            ));

        config.ExposeConfiguration(
                  c => new SchemaExport(c).Execute(true, true, false))
             .BuildConfiguration();
    }

I'm trying to use (new with) Fluent NHibernate (trying to switch from XML mapping file to FNH). With the code below, I generated the database, I'm trying to find the same solution but with FNH (I'd like still use hibernate.cfg.xml) :

public void CreateDatabase()
{
    NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();

    cfg.Configure();
    SchemaMetadataUpdater.QuoteTableAndColumns(cfg);
    NHibernate.Tool.hbm2ddl.SchemaExport schema = new NHibernate.Tool.hbm2ddl.SchemaExport(cfg);

    schema.Create(false, true);
}


namespace MyTestApplication.Entities
{
    public class Person
    {
        public virtual int Id { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
    }
}

namespace MyTestApplication.Data.Mapping
{
    public class PersonMapping : ClassMap<Person>
    {
        public PersonMapping()
        {
            Id(x => x.Id);
            Map(x => x.FirstName);
            Map(x => x.LastName);      
        }
    }
}

Solution

Finally, I use this (Thanks to Marco) :

 public void CreationDB()
    {
        FluentConfiguration config = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008.ConnectionString("......"))
            .Mappings(
                m => m.FluentMappings.Add(typeof(MyTestApplication.Data.Mapping.PersonMapping)
            ));

        config.ExposeConfiguration(
                  c => new SchemaExport(c).Execute(true, true, false))
             .BuildConfiguration();
    }

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

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

发布评论

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

评论(1

_蜘蛛 2024-12-17 17:38:16

我使用这个,即使我认为你可以找到更优雅的东西:

public FluentConfiguration GetConfig()
{
    return Fluently.Configure()
        .Database(
              MySQLConfiguration.Standard.ConnectionString(
                    c => c.Server("...").Database("...").Username("...").Password("..."))
        )
        .Mappings(
              m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())
        );
}

public void Export(bool script, bool export, bool justDrop)
{
    GetConfig()
         .ExposeConfiguration(
              c => new SchemaExport(c).Execute(script, export, justDrop))
         .BuildConfiguration();
}

最后我调用 Export(true, true, false)

I use this, even if I think you could find something more elegant:

public FluentConfiguration GetConfig()
{
    return Fluently.Configure()
        .Database(
              MySQLConfiguration.Standard.ConnectionString(
                    c => c.Server("...").Database("...").Username("...").Password("..."))
        )
        .Mappings(
              m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())
        );
}

public void Export(bool script, bool export, bool justDrop)
{
    GetConfig()
         .ExposeConfiguration(
              c => new SchemaExport(c).Execute(script, export, justDrop))
         .BuildConfiguration();
}

Finally I call Export(true, true, false).

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