问题映射 Fluent NHibernate 的一对多关系

huangapple go评论58阅读模式
英文:

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?

英文:

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; } = &quot;New Asset&quot;;
    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&lt;LogData&gt; Log { get; set; } = new List&lt;LogData&gt;();
}

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 =&gt; x.AssetID,&quot;AssetID&quot;).GeneratedBy.Increment();
        Map(x =&gt; x.Name).Not.Nullable().Default(&quot;\&#39;New Asset\&#39;&quot;);
        Map(x =&gt; x.Value1).Not.Nullable();
        Map(x =&gt; x.Value3).Not.Nullable();
        Map(x =&gt; x.Value4).Not.Nullable();
        Map(x =&gt; x.Value5).Not.Nullable();
        Map(x =&gt; x.Value6).Not.Nullable();
        Map(x =&gt; x.Value7).Not.Nullable();
        Map(x =&gt; x.Value8).Not.Nullable();
        Map(x =&gt; x.Value9).Not.Nullable();
        HasMany(x =&gt; x.Log)
            .AsBag()
            .Cascade.AllDeleteOrphan()
            .KeyColumn(&quot;AssetID&quot;)
            .Not.LazyLoad();
    }

And my LogData Map

public LogDataMap()
    {
        CompositeId()
            .KeyReference(x =&gt; x.Parent, &quot;AssetID&quot;)
            .KeyProperty(x =&gt; x.Timestamp);
        Map(x =&gt; x.Value1).Not.Nullable();
        Map(x =&gt; x.Value2).Not.Nullable();
        Map(x =&gt; 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:

{&quot;Invalid collection name: &#39;Foreign Keys&#39;. (Parameter &#39;collectionName&#39;)&quot;}

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?

答案1

得分: 0

问题原来是表的名称 "Asset"。
我不知道为什么,这可能是与MariaDB特定的问题,或者与NHibernate有关。

无论如何,问题已解决。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月12日 15:58:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定