简体   繁体   English

流利的NHibernate AutoMapping Override忽略列名

[英]Fluent NHibernate AutoMapping Override ignoring column name

Two tables: 两张桌子:

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
)

And the classes 和班级

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>
}

I'm using Auto mapping, and overriding it thusly: 我正在使用自动映射,并因此覆盖它:

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");
    }
}

The problem I have is, when I call Session.Load(id), I get the following error 我遇到的问题是,当我调用Session.Load(id)时,出现以下错误

"could not initialize a collection: " with an Inner Exception "Invalid column name 'hr_information_type_id'" “无法初始化集合:”,并带有内部异常“无效的列名称'hr_information_type_id'”

because it generates the SQL as 因为它生成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=?

I have no idea why it's looking for a column I never said was there, nor why it's looking for any columns twice. 我不知道为什么它要寻找一个我从来没有说过的列,也为什么它要寻找任何列两次。

What am I doing wrong? 我究竟做错了什么? I have very similar code in other projects that are not using Auto Mapping, so is this not the correct way to do the override? 在其他未使用自动映射的项目中,我有非常相似的代码,因此这不是进行覆盖的正确方法吗?

mapping.HasMany(many => many.Errors).AsArray(a => a.Response, part => part.Column("ErrorId")).KeyColumn("ResponseId").Cascade.AllDeleteOrphan(); mapping.HasMany(很多=> many.Errors).AsArray(a => a.Response,part => part.Column(“ ErrorId”))。KeyColumn(“ ResponseId”)。Cascade.AllDeleteOrphan();

It needs an Index and a Key. 它需要一个索引和一个键。 I looked at the HBM files it generated ( using 我查看了它生成的HBM文件(使用

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

)

and saw that. 看到了 The Index is the part.Column("ErrorId") piece, which is the way it indexes into the collection (even though it's just an array). Index是part.Column(“ ErrorId”)部分,这是它索引到集合中的方式(即使它只是一个数组)。 This has gotten me farther, but not 100% sure its the complete answer. 这使我走得更远,但不是100%确定其完整答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM