0

我有一个名为 person_skills 的表,如下所示:

person_id、skill_type_id、base_score、misc_score

有一个查找表,其中包含 id、skill_types 的名称。

现在棘手的是我有一个用于 person_id,skill_type_id 的复合键。由于一个人可能有 5 种技能,因此该表中将有许多条目。

目前我有这样的课程:

public class skill 
{
    int BaseScore {get;set;}
    int MiscScore {get;set;}
}

然后我有一个类来包含所有这些,如下所示:

public class person_skills
{
    int person_id {get;set;}
    IDictionary<skill_type, skill> skills {get;set;}
}

现在我不确定这是否是处理这种关系的最佳方式,最终我需要能够给人们一个技能链接,一个人有很多技能。

我正在考虑只是放入一个自动增量 id 列并将其用作 PK,但这似乎并不理想。如果需要,我可以更改模型和数据库,但由于这是在页面的 ajax 部分中使用的,因此我需要能够更改技能,然后将它们更新到数据库中。

4

1 回答 1

2

我没有找到一个实际的问题,但无论如何我都会回答。:)

person_skills 表不需要代理键。由 person_id 和 Skill_type_id 组成的复合键应该足够了。我相信以下类和映射反映了您在这里要完成的工作。

课程:

public class Person
{
    public virtual int PersonId { get; set; }
    public virtual String Name { get; set; }
    public virtual IList<PersonSkills> Skills { get; set; }
}

public class SkillType
{
    public virtual int SkillTypeId { get; set; }
    public virtual String SkillName { get; set; }
    public virtual IList<PersonSkills> Persons { get; set; }       
}

public class PersonSkills
{
    public virtual int PersonId { get; set; }
    public virtual int SkillTypeId { get; set; }
    public virtual int BaseScore { get; set; }
    public virtual int MiscScore { get; set; }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(this, obj))
        {
            return true;
        }

        if (obj == null || !(obj is PersonSkills))
        {
            return false;
        }
        PersonSkills o = obj as PersonSkills;

        return (this.PersonId == o.PersonId
            && this.SkillTypeId == o.SkillTypeId);
    }

    public override int GetHashCode()
    {
        int hash = 13;
        hash = hash + this.PersonId.GetHashCode();
        hash = hash + this.SkillTypeId.GetHashCode();
        return hash;
    }
}

映射:(FluentNhibernate)

public class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Id(x => x.PersonId);
        Map(x => x.Name);
        HasMany(x => x.Skills)
            .KeyColumn("PersonId")
            .Cascade.All();
    }
}

public class SkillTypeMap : ClassMap<SkillType>
{
    public SkillTypeMap()
    {
        Id(x => x.SkillTypeId);
        Map(x => x.SkillName);
        HasMany(x => x.Persons)
            .KeyColumn("SkillTypeId")
          .Cascade.All();
    }
}

public class PersonSkillsMap : ClassMap<PersonSkills>
{
    public PersonSkillsMap()
    {
        CompositeId()
            .KeyProperty(x => x.PersonId)
            .KeyProperty(x => x.SkillTypeId);
        Map(x => x.BaseScore);
        Map(x => x.MiscScore);
    }
}

映射(hbm,由 FluentNHibernate 生成 - 我删除了不需要的输出):

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >
  <class xmlns="urn:nhibernate-mapping-2.2" name="Person" table="Person">
    <id name="PersonId" type="int">
      <column name="PersonId" />
      <generator class="identity" />
    </id>
    <bag cascade="all" name="Skills" mutable="true">
      <key>
        <column name="PersonId" />
      </key>
      <one-to-many class="PersonSkills" />
    </bag>
    <property name="Name" type="String">
      <column name="Name" />
    </property>
  </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >
  <class xmlns="urn:nhibernate-mapping-2.2" name="SkillType" table="SkillType">
    <id name="SkillTypeId" type="int">
      <column name="SkillTypeId" />
      <generator class="identity" />
    </id>
    <bag cascade="all" name="Persons">
      <key>
        <column name="SkillTypeId" />
      </key>
      <one-to-many class="PersonSkills" />
    </bag>
    <property name="SkillName" type="String">
      <column name="SkillName" />
    </property>
  </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >
  <class xmlns="urn:nhibernate-mapping-2.2" name="PersonSkills" table="PersonSkills">
    <composite-id mapped="false" unsaved-value="undefined">
      <key-property name="PersonId" type="int">
        <column name="PersonId" />
      </key-property>
      <key-property name="SkillTypeId" type="int">
        <column name="SkillTypeId" />
      </key-property>
    </composite-id>
    <property name="BaseScore" type="int">
      <column name="BaseScore" />
    </property>
    <property name="MiscScore" type="int">
      <column name="MiscScore" />
    </property>
  </class>
</hibernate-mapping>
于 2011-03-14T23:33:50.867 回答