Search code examples
c#nhibernatefluent-nhibernate

Fluent Nhibernate how to ignore mapping columns from an existing table


Is it possible to map a class in model to a table that has more columns than the model class? I just want to map specific columns of a table using Fluent Nhibernate. For example the table has these columns:

ProductId
ProductName
BatchNumber
StoreId

but in the model class I just want: ProductId, ProductName. Is it possible not to include BatchNumber and StoreId in the model class?


Solution

  • You can override mapping:

    public class ProductAutoMappingOverride : IAutoMappingOverride<Product> {
    
        public void Override(AutoMapping<Product> mapping) {
           mapping.Id(p => p.ProductId),
           mapping.Map(p => p.ProductName),
           mapping.IgnoreProperty(p => p.BatchNumber);
           mapping.IgnoreProperty(p => p.StoreId);
        }
    
    }