如何用N Hibernate和Fluent.NHibernate Maps建模这个类?

时间:2010-09-23 20:46:38

标签: nhibernate oop fluent-nhibernate

我正在使用带有NHibernate和Fluent.NHibernate Maps的ASP.NET MVC。

我想知道如何在Fluent上映射类并在MySQL上创建数据库表:

public class AgenteDeViagem {
    public virtual int Id { get; set; }
    public virtual string Email { get; set; }
    public virtual AgentePessoa AgentePessoa { get; set; }
}

    public interface AgentePessoa {
}

public class AgenteDeViagemPJ:AgentePessoa {
    public virtual int Id { get; set; }
    public virtual AgenteDeViagem AgenteDeViagem { get; set; }
    public virtual string Razao { get; set; }
}

    public class AgenteDeViagemPF:AgentePessoa {
    public virtual int Id { get; set; }
    public virtual AgenteDeViagem AgenteDeViagem { get; set; }
    public virtual string Nome { get; set; }
}

非常感谢!

1 个答案:

答案 0 :(得分:1)

在我看来,就像你在那里一样。您已经在使用virtual并设置了关系,因此使用自动映射策略,您只需构建session factory

private static ISessionFactory InitializeNHibernate()
{
    var cfg = Fluently.Configure()
        .Database(MySQLConfiguration.Standard.ConnectionString(c =>
            c.Database("agente").Server("localhost")
            .Username("user").Password("password"))) 
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<AgenteDeViagem>())
        .ExposeConfiguration(configuration =>
                                 {
                                     // Comment to disable schema generation
                                     BuildDatabaseSchema(configuration);
                                 });

    return cfg.BuildSessionFactory;
}

private static void BuildDatabaseSchema(Configuration configuration)
{
    var schemaExport = new SchemaExport(configuration);
    schemaExport.SetOutputFile("mysql_script.sql");
    schemaExport.Create(false, true);
}