I used NH 3.2 mapping by code and I tryied Nhibernate Mapping Generator http://nmg.codeplex.com/ which looked a great tool.
I found a big difference between my code and theirs. On each class they have a call to the function LazyLoad(). (Although I thinked that it was the default behaviour)
Now I fear that my application doesn't use lazy loading, does someone know the default behaviour of nh 3.2 with mapping by code ? (when we don't call the LazyLoad method)
Regards
Depends on the default-lazy attribute of the hibernate-mapping tag which can be changed in Fluent NHibernate by adding the DefaultLazy.Always() or DefaultLazy.Never() convention.
If no default-lazy attribute is defined (no convention added in Fluent NHibernate), lazy loading is enabled.
Related
I want to know when we use the key word virtual with navigation properties (I learnt it's for lazy loading) but I'm reading a tutorial in https://docs.asp.net/en/latest/data/ef-mvc/intro.html that creates an asp.net web application core and they are not using that virtual anymore.
I checked on old versions (MVC4, MVC5) it is always there but not in the core.
Can anyone explain to me why?
You use virtual properties on entities, so Entity Framework can create a proxy class at runtime that inherits from your entity and injects a stub into overridden properties. This stub does a database call when you access the property's getter from code.
Entity Framework Core does not support lazy loading (yet, and probably never will), so there's no reason for it to mark properties as virtual.
See also: Loading Related Data - Entity Framework Core 1.0.0 Documentation in the officical documentation, Lazy Loading · Issue #3797 · aspnet/EntityFramework · GitHub on GitHub and Why use 'virtual' for class properties in Entity Framework model definitions? here on Stack Overflow.
Automapping in Nhibernate has been this wonderful magical thing, but now something strange is happening and I want to peel back this magical layer and see the actual mappings that are getting generated.
Is there a way to see the mappings generated by automapper and my overrides so I can see if it's doing what I think it's doing?
If you are using Fluent then you should be able to write the persistence model to the disk:
// In your fluent config code
// assuming config is of type FluentNHibernate.Cfg.FluentConfiguration
var model = new FluentNHibernate.PersistenceModel();
config.Mappings(m => m.UsePersistenceModel(model));
model.WriteMappingsTo(#"C:\some_folder_name_for_hbm_files_to_go_into");
I'm trying to upgrade my FNH Automapping project to the latest versions of NHibernate and Fluent NHibernate (NH 3.2 and FNH 1.3), but now I get a "no persister" exception on a derived class (though the base class still seems to be persisted properly).
This derived class Automapped fine with the old dlls (FNH 1.0, NH 2.1.2) - I have not changed my mapping logic or these classes in any way.
I upgraded my project by just copying the new dlls over the old ones, and deleting references to dlls that are no longer needed (e.g. Antlr 3, Castle) by the new dlls.
Exact versions I'm using:
NHibernate 3.2.0.4000
FluentNHibernate 1.3.0.0
System.Data.SQLite 1.0.76.0
VS 2008 9.0.30729.1 SP
Windows XP SP3 (32 bit)
The mapping code that works with the old dlls, but not with the new ones:
return AutoMap.Assemblies(_assemblies)
// Don't map the abstract base class
.IgnoreBase<OfeEntity>()
// Only map subclasses of OfeEntity
.Where(t => t.IsSubclassOf(typeof(OfeEntity)))
.Conventions.Add(
// Do cascading saves on all entities so lists will be
// automatically saved
DefaultCascade.All(),
// Turn on lazy loading, so will only read data that is actually
// displayed
DefaultLazy.Always()
);
Edit:
After turning FNH Diagnostics on, I can see that FNH is not creating a table for my derived class with the new dlls.
Also, one thing I noticed - the class that is not being persisted is subclassed by 2 levels. That is, I have a the following classes:
public abstract class OfeEntity
public class OfeMeasurementBase : OfeEntity
public class OfeDlsMeasurement : OfeMeasurementBase
OfeDlsMeasurement is the class that is not being persisted. OfeMeasurementBase, as well as several other classes that inherit from OfeEntity, are being persisted properly.
Old versions had no problem with this - maybe new versions have a bug when there's more than one level of inheritance?
I migrated our project from some older (F)NH to the latest NH 3.2. I suspect it now uses different key field names in collections and such, because I need to specify exact column names when using the existing database.
Also, I suggest rebuilding Fluent NHibernate from sources with NHibernate 3.2 just to be sure everything gets in the place.
The article How to upgrade your apps to NHibernate 3.2 with Fluent NHibernate 1.2 may be helpful. I used it myself to upgrade a project and it worked.
in My nhibernate.cfg.xml file I have
<mapping assembly="X.Domain" />
Which would usually works - Inside My X.Domain I have my Fluent Mappings. in which I have tests to verify all the mappings are set up correctly. Not sure if this is because I am using Fluent in my Domain Layer and nhiberante.cfg.xml in my MVC project.
Any ideas
The <mapping/> element is part of NH core; it only recognizes hbm.xml files built as embedded resources, not fluent mappings.
I needed a way to trim strings within my persistent class because my legacy database is using char fields. I downloaded the nHhaddIns dll to use its TrimString class which is derived from IUserType.
Using their example I created a property in my mapping class as shown at the bottom.
uNHAddIns is added as a project within my solution. However, I received this error:"Could not determine type for: uNhAddIns.UserTypes.TrimString, uNhAddIns, for columns: NHibernate.Mapping.Column(HSTAT)"
I tried running the example that is in the uNhAddIns project and receive the same error. Any ideas?
<property name="HSTAT" column="HSTAT" type="uNhAddIns.UserTypes.TrimString, uNhAddIns" />
Don't know if you've managed to fix this already, but does your own uNhAddIns.UserTypes.TrimString inherit from IUserType? My own pattern for user types in NHibernate involves the type implementation living in the DataModel, and the required IUserType interface living separately in my DataAccess layer. The IUserType implementation does the necessary marshalling between the database and my DataModel type implementation.
I just came across this same error when trying to use the DataModel class in my mapping file rather than the IUserType implementation.