我的代码可以正常工作,但是我在表/ddl 中获得了 2 个额外的列,以表示多对多关系,~~但是~~ 具有关系上的属性(标量)。
我正在使用 1.2.0.712 (FluentNHibernate.dll) 3.1.0.4000 (NHibernate.dll)
实体:
public partial class Employee
{
public Employee()
{
CommonConstructor();
}
private void CommonConstructor()
{
this.MyEmployeeToJobTitleMatchLinks = new List<EmployeeToJobTitleMatchLink>();
}
public virtual Guid? EmployeeUUID { get; set; }
public virtual byte[] TheVersionProperty { get; set; }
public virtual string SSN { get; set; }
public virtual string LastName { get; set; }
public virtual string FirstName { get; set; }
public virtual DateTime CreateDate { get; set; }
public virtual DateTime HireDate { get; set; }
public virtual ICollection<EmployeeToJobTitleMatchLink> MyEmployeeToJobTitleMatchLinks { get; set; }
public virtual void AddJobTitleLink(EmployeeToJobTitleMatchLink link)
{
link.TheEmployee = this;
if (!this.MyEmployeeToJobTitleMatchLinks.Contains(link))
{
this.MyEmployeeToJobTitleMatchLinks.Add(link);
}
if (!link.TheJobTitle.MyJobTitleToEmployeeMatchLinks.Contains(link))
{
link.TheJobTitle.MyJobTitleToEmployeeMatchLinks.Add(link);
}
}
public virtual void RemoveJobTitleLink(EmployeeToJobTitleMatchLink link)
{
link.TheEmployee = this;
if (this.MyEmployeeToJobTitleMatchLinks.Contains(link))
{
this.MyEmployeeToJobTitleMatchLinks.Remove(link);
}
if (link.TheJobTitle.MyJobTitleToEmployeeMatchLinks.Contains(link))
{
link.TheJobTitle.MyJobTitleToEmployeeMatchLinks.Remove(link);
}
}
}
public partial class JobTitle
{
public JobTitle()
{
CommonConstructor();
}
private void CommonConstructor()
{
this.MyJobTitleToEmployeeMatchLinks = new List<EmployeeToJobTitleMatchLink>();
}
public virtual Guid? JobTitleUUID { get; set; }
public virtual byte[] TheVersionProperty { get; set; }
public virtual string JobTitleName { get; set; }
public virtual DateTime CreateDate { get; set; }
public virtual ICollection<EmployeeToJobTitleMatchLink> MyJobTitleToEmployeeMatchLinks { get; set; }
public virtual void AddEmployeeLink(EmployeeToJobTitleMatchLink link)
{
link.TheJobTitle = this;
if (!this.MyJobTitleToEmployeeMatchLinks.Contains(link))
{
this.MyJobTitleToEmployeeMatchLinks.Add(link);
}
if (!link.TheEmployee.MyEmployeeToJobTitleMatchLinks.Contains(link))
{
link.TheEmployee.MyEmployeeToJobTitleMatchLinks.Add(link);
}
}
public virtual void RemoveEmployeeLink(EmployeeToJobTitleMatchLink link)
{
link.TheJobTitle = this;
if (this.MyJobTitleToEmployeeMatchLinks.Contains(link))
{
this.MyJobTitleToEmployeeMatchLinks.Remove(link);
}
if (link.TheEmployee.MyEmployeeToJobTitleMatchLinks.Contains(link))
{
link.TheEmployee.MyEmployeeToJobTitleMatchLinks.Remove(link);
}
}
}
public partial class EmployeeToJobTitleMatchLink
{
public EmployeeToJobTitleMatchLink()
{
//this.Id = Guid.NewGuid(); /* this works in conjuction with <generator class="assigned"></generator> */
}
public virtual Guid? LinkSurrogateUUID { get; set; }
/* These are "scalar properties of the ~~relationship~~ */
public virtual int PriorityRank { get; set; }
public virtual DateTime JobStartedOnDate { get; set; }
public virtual Employee TheEmployee { get; set; }
public virtual JobTitle TheJobTitle { get; set; }
}
映射:
public class EmployeeMap : ClassMap<Employee>
{
public EmployeeMap()
{
Id(x => x.EmployeeUUID).GeneratedBy.GuidComb();
OptimisticLock.Version();
Version(x => x.TheVersionProperty)
.Column("MyVersionColumn")
.Not.Nullable()
.CustomSqlType("timestamp")
.Generated.Always();
Map(x => x.SSN);
Map(x => x.LastName);
Map(x => x.FirstName);
Map(x => x.CreateDate);
Map(x => x.HireDate);
HasMany(x => x.MyEmployeeToJobTitleMatchLinks)
.Inverse()
.Cascade.All();
}
}
public class JobTitleMap : ClassMap<JobTitle>
{
public JobTitleMap()
{
Id(x => x.JobTitleUUID).GeneratedBy.GuidComb();
OptimisticLock.Version();
Version(x => x.TheVersionProperty)
.Column("MyVersionColumn")
.Not.Nullable()
.CustomSqlType("timestamp")
.Generated.Always();
Map(x => x.JobTitleName);
Map(x => x.CreateDate);
HasMany(x => x.MyJobTitleToEmployeeMatchLinks)
.Inverse()
.Cascade.All();
}
}
public class EmployeeToJobTitleMatchLinkMap : ClassMap<EmployeeToJobTitleMatchLink>
{
public EmployeeToJobTitleMatchLinkMap()
{
Id(x => x.LinkSurrogateUUID).GeneratedBy.GuidComb();
Map(x => x.PriorityRank);
Map(x => x.JobStartedOnDate);
References(x => x.TheEmployee).Column("TheEmployeeUUID").Not.Nullable();/*Bad naming convention with "The", but left here so it can be seen easily in the DDL*/
References(x => x.TheJobTitle).Column("TheJobTitleUUID").Not.Nullable();/*Bad naming convention with "The", but left here so it can be seen easily in the DDL*/
}
}
这工作正常,但我在 ddl 中获得了 2 个额外的(可为空的)列。它们在下方标有星号 (*)。
Select * From [dbo].[EmployeeToJobTitleMatchLink] LinkSurrogateUUID
PriorityRank JobStartedOnDate
TheEmployeeUUID
TheJobTitleUUID
*Employee_id
*JobTitle_id
我理解这是“按照惯例”。(上面带有“_id”的名称)。但我不需要这些列。而且我需要能够自定义名称。(此模拟示例中的 TheEmployeeUUID 和 TheJobTitleUUID。)
我的最终目标是:
Select * From [dbo].[EmployeeToJobTitleMatchLink] LinkSurrogateUUID (UniqueIdentifier, SurrogateKey)
PriorityRank (scalar, int) JobStartedOnDate (scalar,datetime) TheEmployeeUUID (UniqueIdentifier, FK back to dbo.Employee.EmployeeUUID) TheJobTitleUUID (UniqueIdentifier, FK back to dbo. JobTitle.JobTitleUUID )
~relationship 上的属性非常重要。(此模型示例中的 PriorityRank 和 JobStartedOnDate。)
谢谢。我~那么近。
编辑:
有效的映射:
public class EmployeeMap : ClassMap<Employee>
{
public EmployeeMap()
{
Id(x => x.EmployeeUUID).GeneratedBy.GuidComb();
OptimisticLock.Version();
Version(x => x.TheVersionProperty)
.Column("MyVersionColumn")
.Not.Nullable()
.CustomSqlType("timestamp")
.Generated.Always();
Map(x => x.SSN);
Map(x => x.LastName);
Map(x => x.FirstName);
Map(x => x.CreateDate);
Map(x => x.HireDate);
HasMany(x => x.MyEmployeeToJobTitleMatchLinks)
.Inverse()
.Cascade.All()
.KeyColumn("TheEmployeeUUID")
;
}
}
public class JobTitleMap : ClassMap<JobTitle>
{
public JobTitleMap()
{
Id(x => x.JobTitleUUID).GeneratedBy.GuidComb();
OptimisticLock.Version();
Version(x => x.TheVersionProperty)
.Column("MyVersionColumn")
.Not.Nullable()
.CustomSqlType("timestamp")
.Generated.Always();
Map(x => x.JobTitleName);
Map(x => x.CreateDate);
HasMany(x => x.MyJobTitleToEmployeeMatchLinks)
.Inverse()
.Cascade.All()
.KeyColumn("TheJobTitleUUID")
;
}
}
public class EmployeeToJobTitleMatchLinkMap : ClassMap<EmployeeToJobTitleMatchLink>
{
public EmployeeToJobTitleMatchLinkMap()
{
Id(x => x.LinkSurrogateUUID).GeneratedBy.GuidComb();
Map(x => x.PriorityRank);
Map(x => x.JobStartedOnDate);
References(x => x.TheEmployee).Column("TheEmployeeUUID").Not.Nullable();/*Bad naming convention with "The", but left here so it can be seen easily in the DDL*/
References(x => x.TheJobTitle).Column("TheJobTitleUUID").Not.Nullable();/*Bad naming convention with "The", but left here so it can be seen easily in the DDL*/
}
}
谢谢内森!
PS 我在谷歌搜索/搜索自己时学到的一个新术语是
“客观化的关系”
它在这个页面的评论区: LINK1
万一该页面在未来某个时候死掉,这里是粘贴的评论:
它被称为“对象化关系”(参考:http ://www.orm.net ),在 NIAM/ORM 中,它通常被定义为一种关系,它本身就是一个具有属性的实体。一个客观化的关系总是形成至少一个 m:n 的关系。(来自http://weblogs.asp.net/fbouma/)