[英]Mapping Fluent NHibernate where the primary key should also be a foreign key
[英]Mapping Foreign Key as Primary Key Fluent NHibernate
我有以下表格方案:
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)"}
我究竟做错了什么? 谢谢大家!
通常,这就是我将一对一关系映射的方式:
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.