流畅的NHibernate AutoMapping覆盖忽略列名称

时间:2014-04-11 18:13:42

标签: c# sql nhibernate fluent-nhibernate automapping

两张桌子:

CREATE TABLE [dbo].[Error](
[ErrorId] [int] IDENTITY(1,1) NOT NULL,
[ResponseId] [int] NOT NULL,
<Other fields>

 CONSTRAINT [PK_Error] PRIMARY KEY CLUSTERED 
(
    [ErrorId] ASC
)

CREATE TABLE [dbo].[Response](
[ResponseId] [int] IDENTITY(1,1) NOT NULL,
<other fields>
 CONSTRAINT [PK_Response] PRIMARY KEY CLUSTERED 
(
    [ResponseId] ASC
)

和班级

public partial class ErrorType
{
    public virtual long Id { get; set; }
    public virtual hr_information_type Response { get; set; }
<other fields>
}

public class hr_information_type 
{
    public virtual long Id { get; set; }
    public virtual ErrorType[] Errors { get; set;}
<other fields>
}

我正在使用自动映射,并因此覆盖它:

public class hr_information_typeMap : IAutoMappingOverride<hr_information_type>
{
    public void Override(AutoMapping<hr_information_type> mapping)
    {
        mapping.Table("Response");
        mapping.Id(x => x.Id).Column("ResponseId");
        mapping.HasMany(many => many.Errors).AsArray(a => a.Response, x => x.Column("ResponseId"));         
    }
}

public class ErrorTypeMap : IAutoMappingOverride<ErrorType>
{
    public void Override(AutoMapping<ErrorType> mapping)
    {
        mapping.Table("Error");
        mapping.Id(id => id.Id).Column("ErrorId");
        mapping.References(r => r.Response, "ResponseId");
    }
}

我遇到的问题是,当我调用Session.Load(id)时,我收到以下错误

&#34;无法初始化集合:&#34; 内在的例外 &#34;列名无效&#39; hr_information_type_id&#39;&#34;

因为它生成SQL为

SELECT 
errors0_.hr_information_type_id as hr7_1_, 
errors0_.ErrorId as ErrorId1_, 
errors0_.ResponseId as ResponseId1_, 
errors0_.ErrorId as ErrorId26_0_,
errors0_.ResponseId as ResponseId26_0_ 
FROM Error errors0_ 
WHERE errors0_.hr_information_type_id=?

我不知道为什么它要寻找一个我从未说过的专栏,也不知道为什么它要寻找任何专栏两次。

我做错了什么?我在其他没有使用自动映射的项目中有非常相似的代码,所以这不是正确的覆盖方式吗?

1 个答案:

答案 0 :(得分:0)

mapping.HasMany(many =&gt; many.Errors).AsArray(a =&gt; a.Response,part =&gt; part.Column(&#34; ErrorId&#34;))。KeyColumn(&#34 ; ResponseId&#34)Cascade.AllDeleteOrphan();

它需要索引和密钥。我查看了它生成的HBM文件( 使用

.Mappings(m =>
{
    m.FluentMappings.AddFromAssembly(assembly).ExportTo(@"C:\nh.out");
    m.AutoMappings.Add(am).ExportTo(@"C:\nh.out");
}

看到了。指数是     part.Column(&#34; ErrorID中&#34) piece,它是索引到集合中的方式(即使它只是一个数组)。这让我走得更远,但不是100%肯定它的完整答案。