FluentNHibernate。我需要将 hasmany 关系映射到连接的子类实体类型
我正在尝试让这个 FluentNHibernate 映射发挥作用。我有三个表:人员、雇员和雇主。 Employee表扩展了Person表的属性,它的主键是Person表的外键。
Employee 表还有一个指向 Employer 表的外键。一个雇主可以有很多雇员,每个雇员都是一个人。
我已经使用 FluentNHibernate 来映射这三个表。我对 Employee -> 使用了连接子类映射人。
我试图让映射起作用,以便我可以获取雇主及其关联的雇员(急切地获取),但是生成的 select 语句选择了 Employee 表上的 EmployerId(好),并且人员表(坏)导致错误(见下文)。
选择 employees0_.EmployerId 作为 Employee5_2_、employees0_。 PersonId 为 PersonId2_,employees0_.PersonId 为 PersonId1_1_,employees0_.FirstN 与 FirstName1_1_、employees0_.LastName 相同,如 LastName1_1_、employees0_1_.PayRat e 为 PayRate2_1_、employees0_1_.EmployerId 为 EmployerId2_1_、employer1_.Employe rId 为 EmployerId0_0_,雇主1_。名称为 Name0_0_ FROM [Person] 雇员0_ lef t 将 Employee 员工0_1_ 外连接到员工0_.PersonId=employees0_1_.Employ eeId 左外连接 [雇主] 雇员 0_1_ 上的雇主 1_.EmployerId=雇主 1 .EmployerId WHEREEmployees0.Employee.EmployerId=@p0;@p0 = 1
请在下面找到架构、类和 Fluent 映射。我做错了什么?
我为这些表定义了类,如下所示:
public class Person
{
public int PersonId { get; set; }
public string PersonClassification { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Employer
{
public int EmployerId { get; set; }
public string Name { get; set; }
public IList<Employee> Employees { get; set; }
}
public class Employee : Person
{
public Employer Employer { get; set; }
public decimal PayRate { get; set; }
}
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(x => x.PersonId).Column("PersonId").GeneratedBy.Identity();
Map(x => x.FirstName);
Map(x => x.LastName);
DiscriminateSubClassesOnColumn("PersonClassification");
}
}
public class EmployeeMap : SubclassMap<Employee>
{
public EmployeeMap()
{
this.DiscriminatorValue("Employee");
Join
(
"Employee",
join =>
{
join.Optional();
join.KeyColumn("EmployeeId");
join.Map(x => x.PayRate);
join.References(x => x.Employer).Column("EmployerId");
}
);
}
}
public class EmployerMap : ClassMap<Employer>
{
public EmployerMap()
{
Id(x => x.EmployerId).Column("EmployerId").GeneratedBy.Identity();
Map(x => x.Name);
HasMany(x => x.Employees).KeyColumn("Employee.EmployerId");
}
}
I am trying to get this FluentNHibernate mapping to work. I have three tables Person, Employee and Employer. The Employee table extends the attributes of the Person table, and it's primary key is a foreign key to the Person table.
The Employee table also has a foriegn key to the Employer table. An employer can have many employees, and each employee is a person.
I have used FluentNHibernate to map these three tables. I used a Joined Subclass mapping for Employee -> Person.
I am trying to get a mapping to work so that I can get employers and their associated employees (eager fetching), but the generated select statement selects the EmployerId on both the Employee table (good), and the Person table (bad) resulting in an error (see below).
SELECT employees0_.EmployerId as Employee5_2_, employees0_.
PersonId as PersonId2_, employees0_.PersonId as PersonId1_1_, employees0_.FirstN
ame as FirstName1_1_, employees0_.LastName as LastName1_1_, employees0_1_.PayRat
e as PayRate2_1_, employees0_1_.EmployerId as EmployerId2_1_, employer1_.Employe
rId as EmployerId0_0_, employer1_.Name as Name0_0_ FROM [Person] employees0_ lef
t outer join Employee employees0_1_ on employees0_.PersonId=employees0_1_.Employ
eeId left outer join [Employer] employer1_ on employees0_1_.EmployerId=employer1
.EmployerId WHERE employees0.Employee.EmployerId=@p0;@p0 = 1
Please find the schema, class and Fluent Mappings below. What have I done wrong?
I have defined classes for these tables as follows:
public class Person
{
public int PersonId { get; set; }
public string PersonClassification { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Employer
{
public int EmployerId { get; set; }
public string Name { get; set; }
public IList<Employee> Employees { get; set; }
}
public class Employee : Person
{
public Employer Employer { get; set; }
public decimal PayRate { get; set; }
}
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(x => x.PersonId).Column("PersonId").GeneratedBy.Identity();
Map(x => x.FirstName);
Map(x => x.LastName);
DiscriminateSubClassesOnColumn("PersonClassification");
}
}
public class EmployeeMap : SubclassMap<Employee>
{
public EmployeeMap()
{
this.DiscriminatorValue("Employee");
Join
(
"Employee",
join =>
{
join.Optional();
join.KeyColumn("EmployeeId");
join.Map(x => x.PayRate);
join.References(x => x.Employer).Column("EmployerId");
}
);
}
}
public class EmployerMap : ClassMap<Employer>
{
public EmployerMap()
{
Id(x => x.EmployerId).Column("EmployerId").GeneratedBy.Identity();
Map(x => x.Name);
HasMany(x => x.Employees).KeyColumn("Employee.EmployerId");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论