views:

34

answers:

1

Hi.

I'm using ASP.NET MVC with NHibernate and Fluent.NHibernate Maps.

I would like to know how to map the classes on Fluent and to create the database tables on my 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; }
}

Thank you very much!

+1  A: 

Looks to me like you're halfway there. You're already using virtual and relations are set, so using the Automapping strategy, you only need to build the 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);
}
Rafael Belliard
Thanks, but I'm not using automaping.
MCardinale
Just edited to use fluent mappings. It's pretty much the same...
Rafael Belliard