Ignore property with Fluentnhibernate?

Asked

Viewed 71 times

0

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

trying to.

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 

        }
    }

1 answer

0

So an item is not mapped by Fluentnhibernate, hide the item in configuration class (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, has a FullName that in the class ClientMap has not been configured and will be ignored in create, alter and delete, it is possible to read (single item or list) the property Fullname

In the class you manage should 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:

Browser other questions tagged

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