我的代码工作正常,但是我在表/ ddl中有两个额外的列,表示多对多的关系,~~但是~~关于关系的属性(标量)。
我正在使用 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个额外(可空)列。它们在下面标有星号(*)。
选择*来自[dbo]。[EmployeeToJobTitleMatchLink]
LinkSurrogateUUID
PriorityRank
JobStartedOnDate
TheEmployeeUUID
TheJobTitleUUID
* EMPLOYEE_ID
* JobTitle_id
我明白这是“按照惯例”。 (其上带有“_id”的名称)。 但我不需要这些专栏。我需要能够拥有自定义名称。 (此模拟示例中的TheEmployeeUUID和TheJobTitleUUID。)
我的最终结局是:
选择*来自[dbo]。[EmployeeToJobTitleMatchLink]
LinkSurrogateUUID(UniqueIdentifier,SurrogateKey)
PriorityRank(标量,整数)
JobStartedOnDate(标量,日期时间)
TheEmployeeUUID(UniqueIdentifier,FK返回dbo.Employee.EmployeeUUID)
TheJobTitleUUID(UniqueIdentifier,FK回到dbo.JobTitle.JobTitleUUID)
〜关系中的属性非常重要。 (此模拟示例中的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 我在google / binging时学到的一个新术语是
“客观化的关系”
这是在本页的评论区域: LINK1
如果该页面在将来的某个时间死亡,那么这里是粘贴的注释:
它被称为“客体化关系”(参考:http://www.orm.net),在NIAM / ORM中,它通常被定义为一种关系,它本身就是一个具有属性的实体。客观化的关系总是形成至少一个m:n的关系。 (来自http://weblogs.asp.net/fbouma/)
答案 0 :(得分:1)
我认为您需要将KeyColumn(“key-name”)作为HasMany映射的一部分添加到JobTitleMap和EmplyeeMap。这是因为fluent-nhibernate使用约定在EmployeeToJobTitleMatchLink表中创建FK。使用KeyColumn作为HasMay映射的一部分应该覆盖约定。
如下所示: -
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")**
}
}