Ignore property with FluentNHibernate?

0

I'm trying to ignore a property of a class for mapping with FluentNHibernate and I'm not getting it. I'm trying to follow this example link . How to do this?

Trying.

public class Caixa {

        public virtual long id                      { set; get; }
        public virtual DateTime dtOcorrencia        { set; get; }
        public virtual String historico             { set; get; }
        public virtual int tipoOcorrencia           { set; get; } //1 entrada, 2 saida
        public virtual decimal valorOcorrencia      { set; get; }
        public virtual FormaPagamento formaPagto    { set; get; }

        //ignorar propriedade
        public IList<Caixa> report = new List<Caixa>();

        public Caixa () {
        }

        public IList<Caixa> getReport() {
            return report;
        }

    }

Map

public class CaixaMap : ClassMap<Caixa> {
        public CaixaMap() {
            Table("CAIXA");
            Id(c => c.id).GeneratedBy.Native();
            Map(c => c.dtOcorrencia).CustomType<DateTime>();
            Map(c => c.historico);
            Map(c => c.tipoOcorrencia).CustomType<int>();
            Map(c => c.valorOcorrencia).CustomType<decimal>().Precision(15).Scale(2);            
            Map(c => c.formaPagto).CustomType<GenericEnumMapper<FormaPagamento>>();
            Map(c => c.report); //ignorar 

        }
    }
    
asked by anonymous 20.09.2016 / 20:56

1 answer

0

For an item not to be mapped by FluentNhibernate , hide the item in the class of configuration ( ClassMap ), example:

using FluentNHibernate.Mapping;

namespace Models
{

    public class Client
    {        
        public virtual int Id { set; get; }    
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual string FullName
        {
            get
            {
                return string.Format("{0} {1}", FirstName, LastName);
            }
        }

    }
    public class ClientMap : ClassMap<Client>
    {
        public ClientMap()
        {
            Table("client");

            Id(c => c.Id)
                .GeneratedBy
                .Identity()
                .Column("id")
                .Not.Nullable();

            Map(c => c.FirstName)
                .Column("firstname")
                .Length(15)
                .Not.Nullable();

            Map(c => c.LastName)
                .Column("lastname")
                .Length(50)
                .Not.Nullable();
        }        
    }

}
In the class Client , you have a FullName class ClientMap was not and it will be ignored in create , change and delete

In the class to manage must be configured:

public class Database
{
    public ISession Session { get; private set; }
    public ISessionFactory SessionFactory { get; private set; }     
    public Database()
    {
        SessionFactory = Fluently
            .Configure()
            .Database(MySQLConfiguration.Standard.ConnectionString(c => c.FromConnectionStringWithKey("MyConnectionString")))                
            .Mappings(x => x.FluentMappings.AddFromAssemblyOf<ClientMap>())
            .BuildSessionFactory();

        Session = SessionFactory.OpenSession();
    }
}

Saving record:

Database db = new Database();
Client c = new Client
{
   FirstName = Guid.NewGuid().ToString().Substring(0, 9),
   LastName = Guid.NewGuid().ToString()
};
var trans = db.Session.BeginTransaction();
db.Session.Save(c);            
trans.Commit();
db.Session.Flush();

Reference:

21.09.2016 / 04:05