[英]Fluent NHibernate Mapping a join table without a primary key
I have an existing database that has three tables, two entity tables and a link table. 我有一个具有三个表,两个实体表和一个链接表的现有数据库。 Each of the two entity tables have a primary key, id.
两个实体表中的每个表都有一个主键id。 One uses an int and one uses a guid.
一个使用int,另一个使用guid。 The join table contains two columns, the primary key of each table.
联接表包含两列,每个表的主键。 So, roughly it looks like this:
因此,大致如下所示:
Table 1: Question Id - primary key, int 表1:问题ID-主键,整数
Table 2: Asset Id - primary key, guid 表2:资产ID-主键,GUID
Table 3: QuestionAsset 表3:QuestionAsset
Asset - (PK, FK guid) 资产-(PK,FK GUID)
Question - (PK, FK, int) 问题-(PK,FK,整数)
I have my classes setup as follows: 我的班级设置如下:
public class NQuestion
{
public virtual int Id { get; set; }
...
public virtual IList<NQuestionAsset> Assets { get; set; }
}
public class NQuestionAsset
{
public virtual NAsset Asset { get; set; }
public virtual NQuestion Question { get; set; }
}
public class NAsset
{
public virtual Guid Id { get; set; }
...
}
public class QuestionMap : ClassMap<NQuestion>
{
public QuestionMap()
{
Table("Question");
LazyLoad();
Id(q => q.Id).GeneratedBy.Identity().Column("Id");
...
HasManyToMany(x => x.Assets).Table("QuestionAsset")
.ParentKeyColumns.Add("Asset", "Question")
.Not.LazyLoad();
}
}
public class QuestionAssetMap : ClassMap<NQuestionAsset>
{
public QuestionAssetMap()
{
Table("QuestionAsset");
LazyLoad();
CompositeId().KeyProperty(a => a.Asset, "Id").KeyProperty(a => a.Question, "QuestionId");
References(a => a.Asset.Id).Column("Asset");
References(a => a.Question.QuestionId).Column("Question");
}
}
public class AssetMap : ClassMap<NAsset>
{
public AssetMap()
{
Table("Asset");
LazyLoad();
Id(q => q.Id).GeneratedBy.GuidComb().Column("Id");
...
}
}
I have tried more iterations of relationships than I care to admit to get the link table to populate. 我尝试过很多次关系迭代,而不是允许我填充链接表。 How do I define this relationship?
如何定义这种关系? I can't change the database.
我无法更改数据库。
Right now, with the code above I am getting the following exception: 现在,使用上面的代码,我得到以下异常:
Could not determine type for: Question.Model.NHibernate.NAsset, Question, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(Id)
无法确定类型:Question.Model.NHibernate.NAsset,Question,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null,用于列:NHibernate.Mapping.Column(Id)
In case, 如果,
We should not introduce the pairing object, and just map both ends directly 我们不应该引入配对对象, 只是地图都直接结束
public class NQuestion
{
public virtual int Id { get; set; }
...
//public virtual IList<NQuestionAsset> Assets { get; set; }
public virtual IList<Asset> Assets { get; set; }
and the HasManyToMany
will work 和
HasManyToMany
将工作
public QuestionMap()
{
...
HasManyToMany(x => x.Assets)
.Table("QuestionAsset")
.ParentKeyColumn("Asset")
.ChildKeyColumn("Question")
...;
See more here 在这里查看更多
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.