本文介绍了流利NHibernate连接表中的映射不使用主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从2个表格中创建一个不是通过主键关联的实体

表格:

  CREATE TABLE [employees](
[ssn] [nvarchar](9)NULL,
[active] [bit] NULL,
[employee_id] [int] IDENTITY(1,1)NOT NULL



CREATE TABLE [sam_employees](
[ssn] [nvarchar](9)NULL,
[first_name] [nvarchar](50)NULL,
[last_name] [nvarchar](50)NULL,
[skill] [nvarchar](50 )NULL,
....

生成:

  SELECT this_.employee_id as employee1_0_0_,
this_.ssn as ssn0_0_,
this_.active as active0_0_,
this_1_.first_name as first2_1_0_,
this_1_.last_name as last3_1_0_,
this_1_.skill as skill1_0_
FROM employees this_
inner join sam_employees this_1_
on this_。 ssn = this_1_.ssn
WHERE this_.active = 1

我当前的映射:

  public class EmployeeMap:ClassMap< Employee> 
{
public EmployeeMap()
{
表(employees);
Id(x => x.Id,employee_id);
Map(x => x.SSN,ssn);
Map(x => x.IsActive,active);

加入(sam_employees,mm =>
{
mm.KeyColumn(ssn);
mm.Map(xx => xx。 FirstName,first_name);
mm.Map(xx => xx.LastName,last_name);
mm.Map(xx => xx.Skill,skill);
});






$ b

但是在这个映射中,我有这样的连接条件.employee_id = this_1_.ssn



我知道这个问题以前曾经问过,但是我没有找到一个好的答案,只是解决方法是在数据库端使用View您可以尝试将sam_employees作为实体映射,并使用带有连接类型获取的引用映射。

>

I'm trying to create one entity from 2 tables that are related not by primary key

Tables:

CREATE TABLE [employees](
    [ssn] [nvarchar](9) NULL,
    [active] [bit] NULL,    
    [employee_id] [int] IDENTITY(1,1) NOT NULL 
)


CREATE TABLE [sam_employees](
    [ssn] [nvarchar](9) NULL,
    [first_name] [nvarchar](50) NULL,
    [last_name] [nvarchar](50) NULL,
    [skill] [nvarchar](50) NULL,
    ....
)

SQL to generate:

SELECT this_.employee_id  as employee1_0_0_,
       this_.ssn          as ssn0_0_,
       this_.active       as active0_0_,
       this_1_.first_name as first2_1_0_,
       this_1_.last_name  as last3_1_0_,
       this_1_.skill      as skill1_0_
FROM   employees this_
       inner join sam_employees this_1_
         on this_.ssn = this_1_.ssn
WHERE  this_.active = 1

My current mapping:

public class EmployeeMap : ClassMap<Employee>
{
    public EmployeeMap()
    {
        Table("employees");
        Id(x => x.Id, "employee_id");
        Map(x => x.SSN, "ssn");
        Map(x => x.IsActive, "active");

        Join("sam_employees", mm =>
                                  {                                          
                                      mm.KeyColumn("ssn");
                                      mm.Map(xx => xx.FirstName, "first_name");
                                      mm.Map(xx => xx.LastName, "last_name");
                                      mm.Map(xx => xx.Skill, "skill");
                                  });            
    }
}

But with this mapping I have such join condition on this_.employee_id = this_1_.ssn

I know that this question was asked before but I didn't find a good answer for it, just workaround to use View in database side instead of tables.

解决方案

You can try mapping sam_employees as an entity and use a References mapping with a join type fetch.

这篇关于流利NHibernate连接表中的映射不使用主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 13:30