如何使用 N Hibernate 和 Fluent.NHibernate 映射对此类进行建模?

发布于 2024-09-24 13:14:44 字数 760 浏览 3 评论 0原文

我将 ASP.NET MVC 与 NHibernate 和 Fluent.NHibernate 地图一起使用。

我想知道如何在 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; }
}

非常感谢!

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!

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

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

发布评论

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

评论(1

妄想挽回 2024-10-01 13:14:44

在我看来你已经成功了一半。您已经在使用virtual并且关系已设置,因此使用自动映射策略,您只需要构建会话工厂

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

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