【问题标题】:Mapping Foreign Key as Primary Key Fluent NHibernate将外键映射为主键 Fluent NHibernate
【发布时间】:2014-05-26 16:28:27
【问题描述】:

我有如下表格场景:

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

好的,我正在尝试使用以下代码映射这些实体:

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

好的,当 Fluent NHibernate 尝试引用 AdvertPhotoMapping 类中的 Advert 列并且我不知道 BidMapping CompositeId 是否正确映射时,我遇到了异常。

我得到的例外是:

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

我做错了什么? 谢谢大家!

【问题讨论】:

    标签: c# fluent-nhibernate composite-id


    【解决方案1】:

    通常这就是我映射一对一关系的方式:

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

    您也不需要BidMapping 中的引用映射。它应该是这样的:

    CompositeId()
        .KeyReference(model => model.Advert, "Advert")
        .KeyReference(model => model.User, "User");
    
         Map(model => model.Value).Not.Nullable().Insert().Update();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多