简体   繁体   English

将FK列映射(按代码-不流畅)作为父实体中的枚举列表

[英]mapping (by code - not fluent) an FK column as a list of enums in parent entity

I have an entity class that represents a person and an enum that represents permissions that a person has. 我有一个代表一个人的实体类和一个代表一个人具有的权限的枚举。 I am trying to map this relationship to a database using nhibernate mapping by code without any success. 我试图通过代码使用nhibernate映射将此关系映射到数据库,但没有成功。

The code looks like this: 代码如下:

public enum Permissions
{
  None = 1,
  CanUpdate = 2,
  CanInsert = 3,
  CanDelete = 4
}

public class Person
{
   private ICollection<Permissions> permissions;

   public Person()
   {
      this.permissions = new Collection<Permissions>();
   }

   public virtual ICollection<Permissions> Permissions
   {
      get
      {
         return this.permissions;
      }
   }
}

public class PersonMap : ClassMapping<Person>
{
   public PersonMap()
   {
      this.Set(
        x => x.Permissions,
        m =>
          {
             m.Access(Accessor.Field);
             m.Key(k => k.Column("PersonId"));
             m.Table("PersonHasPermission");
          },
        map => map.Element(
        p =>
          {
             p.Column("PermissionId");
             p.Type<NHibernate.Type.EnumType<Permissions>>();
          }));
    }
}

The database tables look like this: 数据库表如下所示:

Person
-----------------------
PersonId (PK, uniqueidentifier, not null)
Name (nvarchar(max), not null)


PersonHasPermission
-----------------------
PersonId (PK, FK, uniqueidentifier, not null)
PermissionId (PK, FK, int, not null)

So, with this configuration I do not get any exceptions but whenever I try to fetch the permissions for a person the collection is always empty even though there is data in the database. 因此,使用此配置,我不会得到任何异常,但是每当我尝试获取某人的权限时,即使数据库中有数据,集合也始终为空。

I'm hoping that the code above is explains what I am trying to achieve, but do let know if further clarity is required. 我希望上面的代码能够解释我要实现的目标,但是请告知是否需要进一步说明。

Any help with this would be much appreciated. 任何帮助,将不胜感激。 Thanks in advance. 提前致谢。

I am using XML configuration, where I can map even protected or private members. 我正在使用XML配置,甚至可以在其中映射受保护的成员或私有成员。 So, surprising for me is that you are not receiving any Exception. 因此,令我惊讶的是您没有收到任何例外。 Because your Collection does not have any setter (public nor protected). 因为您的收藏集没有任何二传手(公开或受保护的)。 I would expect something like this: NHibernate.PropertyNotFoundException: {"Could not find a setter for property 'Permissions' in class 'Person'"} But maybe fluent is able to translate your mapping from Property to field. 我期望这样的事情: NHibernate.PropertyNotFoundException:{“在类'Person'中找不到属性'Permissions'的设置器”}但也许fluent能够将您的映射从Property转换为Field。

I tried to test your code with a public setter or field (both shown below) and it worked. 我尝试使用公共设置器或字段(都如下所示)测试您的代码,并且该代码有效。 there is my snippet of xml mapping: 有我的xml映射代码段:

<!-- public or protected setter -->
<bag name="Permissions" inverse="false" lazy="true" table="PresonHasPermission" cascade="all">
   <key column="PersonId" />          
   <element type="Permissions" column="PermissionId" />
</bag>

<!-- accessing the field -->
<bag name="permissions" inverse="false" lazy="true" table="PresonHasPermission" cascade="all"
   access="field" >
   <key column="PersonId" />          
   <element type="Permissions" column="PermissionId" />
</bag>

(Leaving your class definition unchanged when mapping to field) (映射到字段时,保持定义不变)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM