How to change the name of the References column by Fluent Nhibernate?

Asked

Viewed 350 times

0

In the "LOG" class, I am trying to enter the column name, which is a FK, using Fluentnhibernate References. I have the following class structure:

public class Pessoa
{
    public virtual int Id { get; set; }

    public virtual string Nome { get; set; }
}

public class PessoaAcesso : Pessoa
{
    public virtual string Login { get; set; }

    public virtual string Senha { get; set; }

    public virtual bool Administrador { get; set; }

    public virtual bool AcessoLiberado { get; set; }

    public virtual IList<Log> Logs { get; set; }

    public PessoaAcesso()
    {
        Logs = new List<Log>();
    }
}

public class Log
{
    public virtual int Id { get; set; }

    public virtual PessoaAcesso PessoaAcesso { get; set; }

    public virtual DateTime DataHora { get; set; }

    public virtual LogAcao Acao { get; set; }

    public virtual string Descricao { get; set; }
}

And their mappings:

public class PessoaMap : ClassMap<Pessoa>
{
    public PessoaMap()
    {
        Id(x => x.Id).GeneratedBy.Identity();

        Map(x => x.Nome)
            .Not.Nullable()
            .Length(100);
    }
}

public class PessoaAcessoMap : SubclassMap<PessoaAcesso>
{
    public PessoaAcessoMap()
    {
        KeyColumn("Id_Pessoa");

        Map(x => x.Login)
            .Not.Nullable()
            .Length(50);

        Map(x => x.Senha)
            .Not.Nullable()
            .Length(100);

        Map(x => x.Administrador)
            .Not.Nullable();

        Map(x => x.AcessoLiberado)
            .Not.Nullable();

        HasMany(x => x.Logs)
            .Cascade.All();
    }
}

public class LogMap : ClassMap<Log>
{
    public LogMap()
    {
        Id(x => x.Id).GeneratedBy.Identity();

        Map(x => x.DataHora)
            .Not.Nullable();

        Map(x => x.Acao)
            .Not.Nullable()
            .CustomType<int>();

        Map(x => x.Descricao)
            .Not.Nullable()
            .Length(200);

        References(x => x.PessoaAcesso)
            .Column("Id_PessoaAcesso")
            .Not.Nullable();
    }
}

When generating the table in SQL Server, it creates the "Personal Idaccess" column correctly, but ALSO creates the "Personal Access_id" column allowing null.

I have tried to enter the column name this way, but you get the same unexpected result:

References(x => x.PessoaAcesso, "Id_PessoaAcesso")

I don’t understand why I’m creating an extra column. What can it be?

  • You can post the mapping of the People class?

  • Added @Fernando

  • SubclassMap does what, changes some mapping behavior? You can post your code as well?

  • I added the full structure @Fernando

  • SubclassMap is equal PessoaMap? What method is KeyColumn("Id_Pessoa");?

  • I am using Subclassmap, because Personal Accessaccess is 1:1 to Person (inheritance). How this is working properly. The Problem is with Log.

  • @Fernando added in Personal Accessomap - Hasmany(x => x.Logs). Keycolumn("Personal Idaccess") and Logmap - References(x => x.Personal Accessaccess). Column("Personal id_access"), and now it worked as expected. I just didn’t understand why I need to indicate in both relations the name of Id. Create an answer with this solution, or edit the question?

  • If your problem has been solved, create an answer with your solution so that others can easily identify the solution to the problem. And it is also clearer the solution so that we can analyze your doubt.

Show 3 more comments

1 answer

1

I indicated the column name for both Personal Map() and Logmap(). It worked.

public class PessoaAcessoMap : SubclassMap<PessoaAcesso>
{
    public PessoaAcessoMap()
    {
        KeyColumn("Id_Pessoa");

        Map(x => x.Login)
            .Not.Nullable()
            .Length(50);

        Map(x => x.Senha)
            .Not.Nullable()
            .Length(100);

        Map(x => x.Administrador)
            .Not.Nullable();

        Map(x => x.AcessoLiberado)
            .Not.Nullable();

        HasMany(x => x.Logs)
            .KeyColumn("Id_PessoaAcesso")
            .Cascade.All();
    }
}

public class LogMap : ClassMap<Log>
{
    public LogMap()
    {
        Id(x => x.Id).GeneratedBy.Identity();

        Map(x => x.DataHora)
            .Not.Nullable();

        Map(x => x.Acao)
            .Not.Nullable()
            .CustomType<int>();

        Map(x => x.Descricao)
            .Not.Nullable()
            .Length(200);

        References(x => x.PessoaAcesso)
            .Column("Id_PessoaAcesso")
            .Not.Nullable();
    }
}
  • Yes now I understood, in Logs, you were not specifying the column name for the Hasmany relation, so you were using the default nomenclature, in this case: class name + "_Id" (Personal access_id), and already in the References relation, you were specifying a name for the column, then he was attending the 2 mappings.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.