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
How to Find Certificate by Its Thumbprint in C#
Catching Exceptions with "Catch, When"
How to Login/Authenticate a User with ASP.NET MVC5 Rtm Bits Using Aspnet.Identity
How to Download/Upload Files From/To Sharepoint 2013 Using Csom
Setting Culture for ASP.NET MVC Application on VS Dev Server and Iis
Dynamically Updating Tabcontrol Content at Runtime
Determine Client's Computer Name
Conversion of a Decimal to Double Number in C# Results in a Difference
C# - Outputting Image to Response Output Stream Giving Gdi+ Error
ASP.NET Identity Cookie Across Subdomains
Differencebetween Property and Dependency Property
Serializable Classes and Dynamic Proxies in Ef - How
String.Empty Converted to Null When Passing JSON Object to MVC Controller
String.Format - How It Works and How to Implement Custom Formatstrings
How to Make Ef-Core Use a Guid Instead of String for Its Id/Primary Key