一对多多列映射

时间:2017-06-07 12:58:44

标签: c# nhibernate fluent-nhibernate nhibernate-mapping fluent-nhibernate-mapping

" One-To-Many"尽管如此,一对一的"一对一"工作正确。 存在2个表:

名称

name_id
organization_id
first_name
last_name

court_case_members

case_id
case_number
member_id
member_organization_id

这两个表是由对引用的!列:

names.name_id =         court_case_members.member_id
names.organization_id = court_case_members.member_organization_id

C#课程:

public class Name
{
    public virtual int Id { get; set; }
    public virtual int OrganizationId { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual IList<CourtCaseMember> Members { get; set; }

    public override bool Equals(object obj)
    {
        var add = obj as Name;
        if (add == null) return false;

        return this.Id == add.Id && this.OrganizationId  == add.OrganizationId;
    }

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }
}

public class CourtCaseMember
{
    public virtual int CaseId { get; set; }
    public virtual string CaseNumber { get; set; }
    public virtual int MemberId { get; set; }
    public virtual int MemberOrganizationId { get; set; }
    public virtual Name Name{ get; set; }

    public override bool Equals(object obj)
    {
        var add = obj as CourtCaseMember;
        if (add == null) return false;

        return this.CaseId == add.CaseId &&
          this.MemberId == add.MemberId &&
          this.MemberOrganizationId  == add.MemberOrganizationId;
    }

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }
}

映射:

public class CourtCaseMemberMapping : ClassMap<CourtCaseMember>
{
    public CourtCaseMemberMapping()
    {
        Table("court_case_members");
        CompositeId()
            .KeyProperty(x => x.MemberId , "member_id")
            .KeyProperty(x => x.MemberOrganizationId , "member_organization_id")
            .KeyProperty(x => x.CaseId , "case_id");
        Map(x => x.CaseNumber, "case_number");
        Map(x => x.MemberId, "member_id");
        Map(x => x.MemberOrganizationId, "member_organization_id");

        References(p => p.Name).Columns("member_id", "member_organization_id");
    }
}


public class NameMapping: ClassMap<Name>
{
    public NameMapping()
    {
        Table("names");
        CompositeId().KeyProperty(x => x.Id, "name_id").KeyProperty(x => x.OrganizationId, "organization_id");
        Map(s => s.OrganizationId, "organization_id");
        Map(s => s.FirstName, "first_name");
        Map(s => s.LastName, "last_name");

        HasMany<CourtCaseMember>(n => n.Members).KeyColumns.Add("member_id", "member_organization_id");
    }
}

问题
当我尝试访问References(p => p.Name).Columns("member_id", "member_organization_id");时,CourtCaseMember.Name一对一映射的工作正常



HasMany<CourtCaseMember>(n => n.Members).KeyColumns.Add("member_id", "member_organization_id");当我尝试访问Name.Members时,一对多映射不起作用,集合不为空,但为空。 如果使用仅包含一列(name_id = member_id)的地图,则可以使用。

我做错了什么?

考虑到@Frédéric评论,我已阅读相关回答问题Minimal and correct way to map one-to-many with NHibernate,我需要写下:(
) 1.提示为inversecascadeall-delete-orphan与UPDATE,INSERT或DELETE操作的行为有关,而与SELECT中这些实体的填充方式无关。

2.当然,我尝试了所有组合:

HasMany<CourtCaseMember>(n => n.Members).KeyColumns.Add("member_id", "member_organization_id").Inverse();
HasMany<CourtCaseMember>(n => n.Members).KeyColumns.Add("member_id", "member_organization_id").Inverse().Cascade.AllDeleteOrphan()

仍然没有结果,IList<CourtCaseMember>集合仍为空事件CourtCaseMember实体与父级有关。

3.当我尝试通过两个字段连接两个实体时,提到的最重要的部分是不起作用:

names.name_id =         court_case_members.member_id
names.organization_id = court_case_members.member_organization_id

如果从:

更改双字段组合键(对于Name实体)
CompositeId()
  .KeyProperty(x => x.Id, "name_id")
  .KeyProperty(x => x.OrganizationId , "organization_id");

Id(x => x.Id, "name_id");

并将关系映射更改为

HasMany<CourtCaseMember>(n => n.Members).KeyColumns.Add("member_id");

它按预期工作,收集已填写,但当然没有member_organization_id提交加入,但这是创建此问题的主要要求。

还有一件事:如果在资源管理器中发现查询,服务器返回相关实体(Members),但它们不会出现在对象模型中。

的语法可能有问题
HasMany<CourtCaseMember>(n => n.Members).KeyColumns.Add("member_id", "member_organization_id")

或其他失败?

在下一次 @Frédéric's 评论后编辑 这是包含测试表和对象(不是来自真实项目)的代码,我犯了一些语法错误。现在它已得到纠正,我已经在DB中创建了这些测试表并进行了真正的测试操作。结果相同=&gt;空对象(标记为红色)。 Mapping

已编辑
答案在评论中。

0 个答案:

没有答案