Fluent nhibernate Fluent NHibernate映射-复合键

Fluent nhibernate Fluent NHibernate映射-复合键,fluent-nhibernate,nhibernate-mapping,s#arp-architecture,automapping,composite-id,Fluent Nhibernate,Nhibernate Mapping,S#arp Architecture,Automapping,Composite Id,我试图在FNH中映射以下表格/实体,但似乎进展很快 **Tables** Contacts ID (PK - int - generated) ... PhoneTypes ID (PK - varchar - assigned) (e.g. MOBILE, FAX) ContactPhones ContactRefId (PK - FK to Contacts) PhoneTypeRefId (PK - FK to PhoneTypes)

我试图在FNH中映射以下表格/实体,但似乎进展很快

**Tables**
Contacts
    ID (PK - int - generated)
    ...

PhoneTypes
    ID (PK - varchar - assigned) (e.g. MOBILE, FAX)

ContactPhones
    ContactRefId    (PK - FK to Contacts)
    PhoneTypeRefId  (PK - FK to PhoneTypes)
    ...
(我应该注意,我也在使用S#arp架构框架)

有人看到我做错了什么吗?我是NH和FNH的新手,从这篇文章中可以明显看出这一点。:-)还有,有人在使用S#arp架构时使用过这样的复合ID吗?什么是最佳实践(除了使用代理键:-)之外)


非常感谢…对于这篇冗长的帖子,我深表歉意。

我也有一段多对多的关系。我的设置如下:

mapping.HasManyToMany(x=>x.Artists).Cascade.All().Inverse().Table(“ArtistImages”)


ArtistImages表具有表艺术家和图像的主键。

我也有多对多关系。我的设置如下:

mapping.HasManyToMany(x=>x.Artists).Cascade.All().Inverse().Table(“ArtistImages”)

ArtistImages表具有表艺术家和图像的主键

**Entities**
public class Contact : Entity
{
    (The ID property is defined in the Entity base class and is type int)

    public virtual ICollection<ContactPhone> PhoneNumbers { get; set; }
}

public class PhoneType : EntityWithTypedId<string>, IHasAssignedId<string>
{
    (The ID property is defined in the base class and is type string)

    ....
}

public class ContactPhone : EntityWithTypedId<ContactPhoneId>, IHasAssignedId<ContactPhoneId>
{
    public virtual Contact Contact { get; set; }

    public virtual PhoneType PhoneType { get; set; }
    ....
}
public class ContactPhoneId : EntityWithTypedId<ContactPhoneId>, IHasAssignedId<ContactPhoneId>
{
    public virtual Contact Contact { get; set; }

    public virtual PhoneType PhoneType { get; set; }
}
...I could just make this class serializable and override 
Equals and GetHashCode myself instead of using the S#arp Arch base class.
public class ContactMap : IAutoMappingOverride<Contact>
{
    public void Override(AutoMapping<Contact> mapping)
    {
        mapping.HasMany<ContactPhone>(x => x.PhoneNumbers)
            .KeyColumns.Add("ContactRefId")
            .KeyColumns.Add("PhoneTypeRefId")
            .AsSet()
            .Inverse()
            .Cascade.All();
    }
}


public class PhoneTypeMap : IAutoMappingOverride<PhoneType>
{
    public void Override(AutoMapping<PhoneType> mapping)
    {
        mapping.Id(x => x.Id).Column("Id").GeneratedBy.Assigned();
    }
}


public class ContactPhoneMap : IAutoMappingOverride<ContactPhone>
{
    public void Override(AutoMapping<ContactPhone> mapping)
    {
        mapping.Table("ContactPhones");
        mapping.CompositeId<ContactPhoneId>(x => x.Id)
            .KeyReference(y => y.Contact, "ContactRefId")
            .KeyReference(y => y.PhoneType, "PhoneTypeRefId");
    }
}  
Foreign key (FK672D91AE7F050F12:ContactPhones [ContactRefId, PhoneTypeRefId])) 
must have same number of columns as the referenced primary key (Contacts [Id])