How to Tell Fluent Nhibernate Not to Map a Class Property

How to tell Fluent NHibernate not to map a class property

map.IgnoreProperty(p => p.What);

Fluent NHibernate ignore property inside the ClassMap, using FluentMappings

I think you are right that the ClassMap is the best place to ignore this property.

Example:

.Override<Shelf>(map =>  
{
map.IgnoreProperty(x => x.YourProperty);
});

Documentation: https://github.com/jagregory/fluent-nhibernate/wiki/Auto-mapping#ignoring-properties

As far as getting the mappings from another assembly, it should be as easy as something like this (depending on your current configuration):

.Mappings(m =>
{
m.FluentMappings.AddFromAssemblyOf<ProvideClassFromYourOtherAssembly>();
});

Fluent NHibernate: How to tell it not to map a base class

If you don't want to automap a class, I would recommend using IAutoMappingOverride<T>. I don't about your database, but it might look like:

public class CarOverride : IAutoMappingOverride<Car>
{

public void Override(AutoMapping<Car> mapping){
mapping.Id( x => x.Id, "CarId")
.UnsavedValue(0)
.GeneratedBy.Identity();

mapping.References(x => x.User, "UserId").Not.Nullable();

mapping.Map(x => x.PlateNumber, "PlateNumber");
// other properties
}
}

Assuming you keep these maps centrally located, you could then load them on your autoMap:

var autoMap = AutoMap.Assembly(assembly).IgnoreBase<EntityBase>().IgnoreBase<EntityBaseValidation>()
.UseOverridesFromAssemblyOf<CarOverride>();

How do I tell Fluent NHibernate to ignore specific properties without automapping?

All properties and methods must be overridable in order for NHibernate to create dynamic proxies, including unmapped properties. This does not imply that NHibernate is mapping your read-only properties, it just requires them to be overridable so that it can generate a proxy of the class. This article explains the requirement.

Prevent mapping all public members of a class in Fluent NHibernate

Fluent NHibernate is not trying to persist all your public members. It's the NHibernate proxying mechanism that requires all members to be virtual; they may or may not be used for persistence, but they're needed anyway.

You either need to disable lazy-loading and proxying for the entity, or (preferably!) expose a DTO in your WS rather than the entity directly.

Fluent NHibernate mapping a class without virtual properties

It specifically mentions GetPassword(), ChangePassword(), and ResetPassword() which are, as far as I can tell, properly overwriting the base class with virtual methods.

No, you didn't overwrite these methods, you've only hidden them.

NHibernate requires all non-private properties and methods to be virtual, otherwise it has no chance to intercept the calls on a proxy.

If you don't need that class to be lazy loaded just map your entity with lazy="false" and you won't need virtual members any more. If you need lazy loading, you can use a proxy interface with proxy="ProxyInterface".

Ignore property mapping by accessbility in Fluent NHibernate AutoMapper

Here's how you tell NHibernate's Autopersistence model to ignore your property:

var cfg = Fluently.Configure()
.Database(configurer)
.Mappings(m =>
{
m.AutoMappings.Add(AutoMap.Assemblies(Assembly.GetExecutingAssembly())
.Override<Team>(map => map.IgnoreProperty(team => team.TeamMembers)));
});

You would then have just what you want.

Mapping a read-only property with no setter using Fluent NHibernate

ReadOnly instructs Fluent NHibernate to not look for changes on this property, this does not equate to a read-only property in compiler-world. Your property isn't read-only in NHibernate's eyes because you're expecting it to be populated from your database. What you need to do is tell NHibernate that it should access the value of that property through a private field with the same name (lowercased) as the property.

Map(x => x.LastUpdate)
.Access.Field();

There are several alternatives to using Field, which one you use will depend on how you name your private fields.



Related Topics



Leave a reply



Submit