开发者

How to model this classes withN Hibernate and Fluent.NHibernate Maps?

开发者 https://www.devze.com 2023-01-17 14:14 出处:网络
I\'m using ASP开发者_StackOverflow社区.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:

I'm using ASP开发者_StackOverflow社区.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!


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

精彩评论

暂无评论...
验证码 换一张
取 消