【问题标题】:How to model this classes withN Hibernate and Fluent.NHibernate Maps?如何使用 N Hibernate 和 Fluent.NHibernate Maps 对此类进行建模?
【发布时间】:2010-09-29 04:13:18
【问题描述】:

我将 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; }
}

非常感谢!

【问题讨论】:

    标签: nhibernate oop fluent-nhibernate


    【解决方案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);
    }
    

    【讨论】:

    • 谢谢,但我没有使用自动映射。
    猜你喜欢
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 2011-08-19
    相关资源
    最近更新 更多