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

标签 database nhibernate configuration fluent-nhibernate

我正在尝试使用(新的)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);      
        }
    }
}

解决方案

最后,我使用这个(感谢 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();
    }

最佳答案

我使用它,即使我认为您可以找到更优雅的东西:

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)

关于database - 使用 Fluent NHibernate 使用 Nhibernate 生成数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7793095/

相关文章:

python - SQL 不显示更新的表

c# - NHibernate 中的非锁定事务

NHibernate多对多在插入前删除所有关联

c# - NHibernate:多对多映射异常

.net - 即使缺少依赖 dll 也能启动 exe 吗?

mysql - 映射产品和商店的策略,也许还有国家?

c++ - 在 C++ 应用程序中存储、跟踪和更新 SQLite 数据库版本

java - 解析数据库用户密码重置

java - Grails 与另一个 Spring 应用程序的集成 - 数据源过载

Android:防止屏幕方向改变但仍会收到通知