問題が発生しました。これはNHibernateの欠陥だと思います。私のクラスMapping-By-CodeはBagPropertyMapperの列名を無視します
TABLE Company
(
ID BIGINT PRIMARY KEY
)
TABLE CompanyMailRecipient
(
ID BIGINT PRIMARY KEY IDENTITY(1, 1),
Company_ID BIGINT NOT NULL FOREIGN KEY REFERENCES Company(id),
Name VARCHAR(MAX),
EmailAddress VARCHAR(MAX),
DestinationType TINYINT
)
:シンプルな親子のマッピングを含む
私のデータベーススキーマ、。 CompanyMailRecipient
テーブルにはEmailAddress
という列がありますが、MailRecipient
クラスにはAddress
という列があります。私は会社を照会しようとしたとき、私は(太字で重要な部分で)次のエラーを取得する
NHibernateは:
public enum MessageDestinationType { Normal = 1, CC = 2, BCC = 3 } public class MailRecipient { public virtual string Name {get; set } public virtual string Address {get; set; } // Different name to the column! public virtual MessageDestinationType DestinationType {get; set;} } public class MailConfiguration { private Lazy<IList<MailRecipient>> _recipients = new Lazy<IList<MailRecipient>>(() => new List<MailRecipient>()); public virtual IList<MailRecipient> Recipients { get { return _recipients.Value; } set { _recipients = new Lazy<IList<MailRecipient>>(() => value); } } } public class Company { public virtual long Id { get; set; } public virtual MailConfiguration MailConfiguration { get; set; } }
マッピングコード
mapper.Class<Company>( classMapper => { classMapper.Table("Company"); classMapper.Component( company => company.MailConfiguration, componentMapper => { componentMapper.Bag(mc => mc.Recipients, bagPropertyMapper => { bagPropertyMapper.Table("CompanyMailRecipient"); bagPropertyMapper.Key(mrKeyMapper => { mrKeyMapper.Column("Company_Id"); }); }, r => r.Component( mrc => { mrc.Property ( mr => mr.Name, mrpm => mrpm.Column("Name") ); /*****************************/ /* Here's the important bit */ /*****************************/ mrc.Property ( mr => mr.Address, mrpm => mrpm.Column("EmailAddress"); ); mrc.Property ( mr => mr.DestinationType, mrpm => mrpm.Column("DestinationType") ); }; ) ); } }
は今ここに問題があります。 Exceptions.GenericADOException:コレクションを初期化できませんでした:[Kiosk.Server.Entities.Company.MailConfiguration.Recipients#576] [SQL:Recipients 0_.Company_IdをCompany1_0_、recipients0_.NameをName0_、recipients0_.Address as Address0_、recipients0_estinationType Destinat4_0_ CompanyMailRecipient受信者から_受信者0_.Company_Id =?] ----> System.Data.SqlClient.SqlException:無効な列名 'Address'。 NHibernate.Loader.Loader.LoadCollection(ISessionImplementorセッション、オブジェクトID、ITYPEタイプ)
で しかし、私のMailRecipient
クラスはEmailAddress
代わりのAddress
と呼ばれるproperyを持っているので、私はC#のコードを変更した場合、すべてが動作します。
NHibernateが私の列マッピングを無視しているようです。
これはNHibernateのバグですか、それとも何か不足していますか?
私が使用しているNHibernateのバージョンは4.0.4.4です。
上記の例では、1対多のコンポーネントがエンティティからハングしているコンポーネントにぶら下がっています。
私は、インジェクションのレイヤーを削除して、自分の1対多のコンポーネントがエンティティに直接ぶら下がっていた場合、カラム名が有効になることがわかりました。
あなたはMailRecipientのIDを持っているので、これはコンポーネントのマッピングのようには見えません。 https://blog.gisspan.com/2011/10/nhibernate-mapping-map-list-of.html – Fran
@Fran - NHibernateはこのIDについて知らず、SQL Serverによって自動的に生成されます。何も効果がありません。 –