napfernandes
napfernandes

Reputation: 1359

Mapping Foreign Key as Primary Key Fluent NHibernate

I have the following table scenario:

User
    - Id       (PK)
    - Username 

Advert
    - Id    (PK)
    - Title

AdvertPhoto
    - Advert (PK) (Also a FK for Advert table)
    - Image

Bid
    - Advert (PK) (Also a FK for Advert table)
    - User   (PK) (Also a FK for User table)
    - Value

Ok, I'm trying to map these entities with the code below:

public class AdvertMapping : BaseMapping<Advert> {
    public AdvertMapping() : base("Advert") {
        Id(model => model.Id).Not.Nullable().Unique().GeneratedBy.Identity();
        Map(model => model.Title).Not.Nullable().Length(100).Insert().Update();
    }
}

public class AdvertPhotoMapping : BaseMapping<Advert> {
    public AdvertPhotoMapping() : base("AdvertPhoto") {
        Id(model => model.Advert)
            .Column("Id")
            .GeneratedBy.Foreign("Advert");

        Map(model => model.Description).Nullable();
        Map(model => model.Photo).CustomSqlType("image").Not.Nullable();
        References(model => model.Advert, "Advert").Not.Nullable().Not.LazyLoad();
    }
}

public class BidMapping { //: BaseMapping<Bid> {
    public BidMapping() : base("Bid") {
        Id(model => model.Advert, "Advert").GeneratedBy.Foreign("Advert");
            CompositeId()
                .KeyReference(model => model.Advert, "Advert")
                .KeyReference(model => model.User, "User");

            Map(model => model.Value).Not.Nullable().Insert().Update();
            References(model => model.Advert, "Advert").Not.Nullable().Not.LazyLoad();
    }
}

Ok, I'm getting an exception when the Fluent NHibernate is trying to reference the Advert column in the AdvertPhotoMapping class and I don't know if the BidMapping CompositeId is mapped properly.

The exception I got is:

{"Could not determine type for: SYB.Engine.Entities.Advert, SYB.Engine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(Advert)"}

What am I doing wrong? Thanks all!

Upvotes: 2

Views: 3098

Answers (1)

Cole W
Cole W

Reputation: 15313

Generally this is how I would map one to one relationships:

public class AdvertPhotoMapping : BaseMapping<Advert> {
    public AdvertPhotoMapping() : base("AdvertPhoto") {
        Id(model => model.Advert)
            .Column("Id")
            .GeneratedBy.Foreign("Advert");

        Map(model => model.Description).Nullable();
        Map(model => model.Photo).CustomSqlType("image").Not.Nullable();
        HasOne(model => model.Advert).Constrained();
    }
}

public class AdvertMapping : BaseMapping<Advert> {
    public AdvertMapping() : base("Advert") {
        Id(model => model.Id).Not.Nullable().Unique().GeneratedBy.Identity();
        Map(model => model.Title).Not.Nullable().Length(100).Insert().Update();
        HasOne(x => x.AdvertPhoto).Cascade.All();
    }
}

Also you shouldn't need the References mapping in BidMapping. It should look like this:

CompositeId()
    .KeyReference(model => model.Advert, "Advert")
    .KeyReference(model => model.User, "User");

     Map(model => model.Value).Not.Nullable().Insert().Update();

Upvotes: 1

Related Questions