我有以下表和实体需要在Fluent NHibernate中映射。
表:
CREATE TABLE workarea
(
id uuid NOT NULL,
name character varying(255) NOT NULL,
CONSTRAINT pk_workarea PRIMARY KEY (id),
)
CREATE TABLE element
(
id uuid NOT NULL,
name character varying(255) NOT NULL,
CONSTRAINT pk_element PRIMARY KEY (id),
)
CREATE TABLE attachment
(
id uuid NOT NULL,
filename character varying(255) NOT NULL,
CONSTRAINT pk_attachment PRIMARY KEY (id),
)
CREATE TABLE objectattachment
(
id uuid NOT NULL,
attachmentid uuid NOT NULL,
attachmenttype string NOT NULL,
objectid uuid NOT NULL,
CONSTRAINT pk_objectattachment PRIMARY KEY (id),
CONSTRAINT fk_oa_a FOREIGN KEY (attachmentid)
REFERENCES attachment (id) MATCH SIMPLE
ON UPDATE RESTRICT ON DELETE RESTRICT,
CONSTRAINT fk_oa_at FOREIGN KEY (attachmenttypeid)
REFERENCES attachmenttype (id) MATCH SIMPLE
ON UPDATE RESTRICT ON DELETE RESTRICT
)
此数据库设计下的想法如下:
“工作区”或“元素”可能有多个“附件”文件,“附件”文件可能会由多个“工作区”或“元素”引用。
“工作区”或“元素”可以指同一个“附件”文件。
所以“附件”和“工作区”或“元素”之间的关系存储在“对象附件”中 “表,其中:
“attachmentid”字段是指特定“附件”的标识符。
“attachmenttype”字段(discriminator)定义了这种关系 在“附件”和“工作区”之间或“附件”和“附件”之间 “元件” 第
“objectid”字段是指特定“工作区”或“元素”的标识符,具体取决于上述“attachmenttype”字段的值。
基于数据库设计,我然后按如下方式定义域模型类:
public class WorkArea
{
private Guid _id = Guid.Empty;
private string _name;
public virtual Guid Id
{
get { return _id ; }
set { _id = value; }
}
public virtual string Name
{
get { return _name ; }
set { _name = value; }
}
}
public class Element
{
private Guid _id = Guid.Empty;
private string _name;
public virtual Guid Id
{
get { return _id ; }
set { _id = value; }
}
public virtual string Name
{
get { return _name ; }
set { _name = value; }
}
}
public class Attachment
{
private Guid _id = Guid.Empty;
private string _fileName;
public virtual Guid Id
{
get { return _id ; }
set { _id = value; }
}
public virtual string FileName
{
get { return _fileName; }
set { _fileName= value; }
}
}
public class WorkAreaAttachment : Attachment
{
private WorkArea _workArea;
public virtual WorkArea WorkArea
{
get { return _workArea; }
set { _workArea = value; }
}
}
public class ElementAttachment : Attachment
{
private Element _element;
public virtual Element Element
{
get { return _element; }
set { _element = value; }
}
}
现在我的问题是我是否可以使用上述数据库设计映射这些域模型类。如果是,那我怎么能这样做?如果不是,那么如何更改域模型类以支持针对设计数据库的Fluent NHibernate映射,因为我不想更改当前数据库设计(即为“workarea”和“element”创建单独的“附件”表)
此致 泉
答案 0 :(得分:0)
public class AttachmentLink
{
private Attachment _attachment;
public virtual Attachment Parent
{
get { return _attachment; }
set { _attachment = value; }
}
private IHasAttachments _linkedTo;
public virtual IHasAttachments LinkedTo
{
get { return _linkedTo; }
set { _linkedTo = value; }
}
}
// in AttachmentMap
HasMany(x => x.Links)
.Table("objectattachment");
// map the component
sealed class AttachmentLinkMap : ComponentMap<AttachmentLink>
{
public AttachmentLinkMap()
{
References(x => x.Attachment, "attachmentid");
ReferencesAny(x => x.LinkedTo)
.IdentityType<Guid>()
.EntityIdentifierColumn("objectid")
.EntityTypeColumn("attachmenttype")
.AddMetaValue<WorkArea>(typeof(WorkArea).Name.ToLower())
.AddMetaValue<Element>(typeof(Element).Name.ToLower())
.Not.LazyLoad(); // to prevent false proxies
}
}
// in ElementMap, and almost the same in WorkAreaMap
HasManyToMany(x => x.Attachments)
.Where("attachmenttype='element'")
注意:您不需要链接表中的Id列