Description
I have a strange failure if i use NHibernate in my application in .NET 7 (port from .NET 6). I have made a littel Visual Studio 2022 solution to reproduce that behaviour and attached it.
In the VS solution there are 3 projects:
- Common
- ConsoleNet6
- ConsoleNet7
In the project Common exists a SQLite database testdb.sqlite with two tables (but no rows):
Table Derived_Class:
- Id
- ClassProp1
- CLASS_PROP_2
- BASE_PROP_1
- BASE_PROP_2
- BASE_PROP_3
Table Derived_Class_2:
- Id
- ClassProp1
- CLASS_PROP_2
NB: the naming with and without underscores was just for testing the mappings!
These tables are mapped to classes as follows:
public partial class DerivedClassMap : ClassMapping<DerivedClass>
{
public DerivedClassMap()
{
Table("DERIVED_CLASS");
DynamicInsert(false);
DynamicUpdate(false);
Lazy(false);
Id(x => x.Id, m =>
{
m.Generator(Generators.Assigned);
});
Property(x => x.ClassProp1);
Property(x => x.ClassProp2, m => m.Column("CLASS_PROP_2"));
Property(x => x.BaseProp1, m => m.Column("BASE_PROP_1"));
Property(x => x.BaseProp2, m => m.Column("BASE_PROP_2"));
Property(x => x.BaseProp3, m => m.Column("BASE_PROP_3"));
ExtendMapping();
}
partial void ExtendMapping();
}
public partial class DerivedClass2Map : ClassMapping<DerivedClass2>
{
public DerivedClass2Map()
{
Table("DERIVED_CLASS_2");
DynamicInsert(false);
DynamicUpdate(false);
Lazy(false);
Id(x => x.Id, m =>
{
m.Generator(Generators.Assigned);
});
Property(x => x.ClassProp1);
Property(x => x.ClassProp2, m => m.Column("CLASS_PROP_2"));
ExtendMapping();
}
partial void ExtendMapping();
}
Both classes are derived from ObjectBase ...
public abstract class ObjectBase
{
public string BaseProp1 { get; set; }
public string BaseProp2 { get; set; }
public string BaseProp3 { get; set; }
}
public partial class DerivedClass : ObjectBase
{
#region [ Public Members ]
public int Id { get; set; }
public string ClassProp1 { get; set; }
public string ClassProp2 { get; set; }
#endregion
...
}
public partial class DerivedClass2 : ObjectBase
{
#region [ Public Members ]
public int Id { get; set; }
public string ClassProp1 { get; set; }
public string ClassProp2 { get; set; }
#endregion
...
}
...but as you can see in the mapping, no property from the abstract base class is mapped for DerivedClass2.
If i run the ConsoleNet7 project within Visual Studio 2022 (with or without debugging) the line
Log.Logger.Information("Fetching from table \"DERVIED_CLASS\": all properties mapped (also that from the abstract base class)");
var derivedClasses = _session.Query<DerivedClass>().ToList();
executes correct and the line
Log.Logger.Information("Fetching from table \"DERVIED_CLASS_2\": no base class properties mapped");
var derivedClasses2 = _session.Query<DerivedClass2>().ToList();`
which resides in the DummyService class in the project Common, produces the following error message:
01.12.2022 17:17:34.664 +01:00 [ ERR] [NHibernate.AdoNet.AbstractBatcher] Could not execute query: select derivedcla0_.Id as id1_0_, derivedcla0_.ClassProp1 as classprop2_0_, derivedcla0_.CLASS_PROP_2 as class3_0_, derivedcla0_.BASE_PROP_1 as base4_0_, derivedcla0_.BASE_PROP_2 as base5_0_, derivedcla0_.BASE_PROP_3 as base6_0_ from DERIVED_CLASS_2 derivedcla0_
code = Error (1), message = System.Data.SQLite.SQLiteException (0x800007BF): SQL logic error
no such column: derivedcla0_.BASE_PROP_1
And that is correct...there is no column named BASE_PROP_1 on table DERIVED_CLASS_2 only on DERIVED_CLASS !!
So why is this statement generated this way and why with the correct (!!) column name from table DERIVED_CLASS ??
If i run the same statements within the .NET 6 console project, then the result or behaviour is correct and no error is displayed for the second select:
01.12.2022 17:38:35.745 +01:00 [ INF] [] Application Starting
01.12.2022 17:38:36.680 +01:00 [ INF] [] Fetching from table "DERVIED_CLASS": all properties mapped (also that from the abstract base class)
01.12.2022 17:38:36.880 +01:00 [ DBG] [NHibernate.SQL] select derivedcla0_.Id as id1_1_, derivedcla0_.ClassProp1 as classprop2_1_, derivedcla0_.CLASS_PROP_2 as class3_1_, derivedcla0_.BASE_PROP_1 as base4_1_, derivedcla0_.BASE_PROP_2 as base5_1_, derivedcla0_.BASE_PROP_3 as base6_1_ from DERIVED_CLASS derivedcla0_
NHibernate: select derivedcla0_.Id as id1_1_, derivedcla0_.ClassProp1 as classprop2_1_, derivedcla0_.CLASS_PROP_2 as class3_1_, derivedcla0_.BASE_PROP_1 as base4_1_, derivedcla0_.BASE_PROP_2 as base5_1_, derivedcla0_.BASE_PROP_3 as base6_1_ from DERIVED_CLASS derivedcla0_
01.12.2022 17:38:36.894 +01:00 [ INF] [] Fetching from table "DERVIED_CLASS_2": no base class properties mapped
01.12.2022 17:38:36.895 +01:00 [ DBG] [NHibernate.SQL] select derivedcla0_.Id as id1_0_, derivedcla0_.ClassProp1 as classprop2_0_, derivedcla0_.CLASS_PROP_2 as class3_0_ from DERIVED_CLASS_2 derivedcla0_
NHibernate: select derivedcla0_.Id as id1_0_, derivedcla0_.ClassProp1 as classprop2_0_, derivedcla0_.CLASS_PROP_2 as class3_0_ from DERIVED_CLASS_2 derivedcla0_
I also get these behaviour if i use Oracle or SQL Server but for testing and submitting the SQLite Version is better.
And now the absolut strange thing....
If i execute either the .NET6 or .NET7 console app directly in a command prompt on my machine, then both of them, also the .NET7 one, are working correct !!! The described error is only within Visual Studio 2022 targeting .NET 7 present.
I also checked with a complete clean install of Windows 11 (all patches) and a clean install of Visual Studio 2022 and got the same result.
Maybe i am missing something, but i don't know what.