Entity with simple key referencing another with key composed

1

I'm using Fluent NHibernate to map my classes, and I have the following situation:

UserMap class

public class UsuarioMap : ClassMap<Usuario>
{
    public UsuarioMap()
    {
        Table("USUARIOS");
        Id(x => x.ID, "USUARIO_ID").GeneratedBy.Increment();
        //Comentado para evitar código desnecessário

        References(x => x.Proprietario).Columns("PROPRIETARIO_ID", "SERVIDOR_ID");
    }
}

ProprietaryMap class:

public class ProprietarioMap : ClassMap<Proprietario>
{
    public ProprietarioMap()
    {
        ReadOnly();

        Table("PROPRIETARIOS");
        CompositeId().
            KeyProperty(x => x.ID, "PROPRIETARIO_ID").
            KeyProperty(x => x.ServidorId, "SERVIDOR_ID");

        Id(x => x.ID, "PROPRIETARIO_ID").GeneratedBy.Assigned();

        //Comentado para evitar código desnecessário
    }
}

When trying to save a Usuario , I get a ArgumentOutOfRangeException . The object that I try to save is as follows:

var usuario = new Usuario
{
    Proprietario = new Proprietario { ID = 1497196, ServidorId = 2 }
    // Comentado para evitar código desnecessário
};

It's interesting that if I create a ProprietarioID property in the Usuario class and map it, the object is saved without any problems.

What am I doing wrong? Is mapping a composite entity to a single entity correct?

    
asked by anonymous 13.05.2014 / 21:50

1 answer

1

I solved the question by mapping a private field called proprietarioID , and pointing the map to it:

Map(x => x.ProprietarioID).Column("PROPRIETARIO_ID").Access.CamelCaseField(Prefix.None);'

In class Usuario , I changed the constructor to receive a parameter of type Proprietario , with at least ID and ServidorID , and property Proprietario now only has get, where I return the object of the bank . In any case, thank you very much @Fanando for the help!

 public virtual Proprietario Proprietario
 {
     get
     {
         if (proprietario != null)
             return proprietario;

         if (proprietarioID == 0 || ServidorID == 0) return null;
         proprietario = new Repository<Proprietario>().Get(proprietarioID, SrvUn);
         return proprietario;
     }
 }

 public Usuario() : this(null)
 {

 }

 public Usuario(Proprietario proprietario)
 {
     if (proprietario == null) return;

     proprietarioID = proprietario.ID;
     this.proprietario = proprietario;
 }
    
14.05.2014 / 20:17