开发者

Fluent NHibernate, Many-to-Many, setting a property on the child from the many-to-many table

I have a Subscriber object that contains a list of Provider objects. Providers can belong to many Subscribers, hence the many-to-many relationship. This is fine, except that the Provider needs to define a Status property but this cannot be stored in the Provider table, as the same provider could have a different Status for different Subscribers, so I am storing the Status in the many-to-many table. At the moment I have a basic many-to-many mapping:

HasManyToMany(s => s.Providers)
    .Table("Su开发者_如何学GobscriberProviders")
    .ParentKeyColumn("SubscriberID")
    .ChildKeyColumn("ProviderID");

How can I set the Status property, of the Provider, within the many-to-many mapping?

Many thanks


A many-to-many mapping can't have properties of its own, so you have to map the join table into an artificial ProviderSubscriber entity, which will be one-to-many from the Provider.

For a full example of a workaround, see Many-to-many relationships with properties


You'll have to map the cross-reference table (which NH currently generates for you), and change the mapping between Providers and Subscribers to instead be a HasMany() on either side referencing the cross-reference table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜