databaseormnhibernatemappingfluent

Problem mapping Fluent NHibernate One-To-Many


I am having issues mapping two classes to a database via Fluent NHibernate. My classes are Asset and LogData.

Asset has a bunch of decimal configuration values, and a Log which is a LogData.

public class Asset
{
    public virtual int AssetID { get; set; }
    public virtual string Name { get; set; } = "New Asset";
    public virtual double Value1 { get; set; }
    public virtual double Value2 { get; set; }
    public virtual double Value3 { get; set; }
    public virtual double Value4 { get; set; }
    public virtual double Value5 { get; set; }
    public virtual double Value6 { get; set; }
    public virtual double Value7 { get; set; }
    public virtual double Value8 { get; set; }
    [Range(0, 100)]
    public virtual double Value9 { get; set; }
    public virtual IEnumerable<LogData> Log { get; set; } = new List<LogData>();
}

LogData has a bunch of decimal values for what it has measured, and a timestamp.

public class LogData
{
    public virtual Asset Parent { get; set; } = new();
    public virtual DateTime Timestamp { get; set; }
    public virtual double Value1 { get; set; }
    public virtual double Value2 { get; set; }
    public virtual double Value3 { get; set; }
    public override bool Equals(object? obj){...}
    public override int GetHashCode(){...}
    }
}

Here is my Asset Map

public AssetMap()
    {
        Id(x => x.AssetID,"AssetID").GeneratedBy.Increment();
        Map(x => x.Name).Not.Nullable().Default("\'New Asset\'");
        Map(x => x.Value1).Not.Nullable();
        Map(x => x.Value3).Not.Nullable();
        Map(x => x.Value4).Not.Nullable();
        Map(x => x.Value5).Not.Nullable();
        Map(x => x.Value6).Not.Nullable();
        Map(x => x.Value7).Not.Nullable();
        Map(x => x.Value8).Not.Nullable();
        Map(x => x.Value9).Not.Nullable();
        HasMany(x => x.Log)
            .AsBag()
            .Cascade.AllDeleteOrphan()
            .KeyColumn("AssetID")
            .Not.LazyLoad();
    }

And my LogData Map

public LogDataMap()
    {
        CompositeId()
            .KeyReference(x => x.Parent, "AssetID")
            .KeyProperty(x => x.Timestamp);
        Map(x => x.Value1).Not.Nullable();
        Map(x => x.Value2).Not.Nullable();
        Map(x => x.Value3).Not.Nullable();
    }

When I start my application, the first thing that happens is a SchemaValidator().Validate() is run, which discovers that the Asset and LogData tables do not exist, and calls a SchemaUpdate.Execute(false, true). The Schema Update encounters an exception though:

{"Invalid collection name: 'Foreign Keys'. (Parameter 'collectionName')"}

I have tried pretty much everything I can think of. I have tried removing the Asset reference from the LogData's Composite ID and just having it as a References, I have tried removing all mapping settings from Asset's HasMany, heck, I have tried entirely removing all references between the Asset and the LogData and just treating them as entirely separate tables with no knowledge of each other, but every time the end-result is that same exception; it catches the same invalid Foreign Keys collection, even if the entire database model has NO foreign keys whatsoever.

With these kinds of errors, typically, the answer is that I am missing something obvious, but it'll take a different pair of eyes than mine to see it. Help?


Solution

  • The issue turned out to be the name of the table "Asset". I have no idea why, this may be an issue specific to MariaDB, or it may be something to do with NHibernate.

    Regardless, the problem is solved.