NHibernate 从一对多到一对一

发布于 2024-09-01 01:55:15 字数 850 浏览 9 评论 0原文

我正在为正在创建的软件创建安全系统,我想知道如何在 NHibernate 中执行以下操作(如果可能的话)

用户帐户类别:

public class UserAccount
{
    public virtual int UserID { get; set; }
    public virtual String Username { get; set; }
    public virtual Privilege InvoicePrivilege { get; set; }
    public virtual Privilege TradePrivilege { get; set; }
}

权限类别:

public class Privilege
{
    public virtual bool Read { get; set; }
    public virtual bool Write { get; set; }
    public virtual bool Delete { get; set; }
    public virtual bool Modify { get; set; }
}

我将拥有大量这些权限对象(每个交易实体一个),因此在数据库中我有以下表格:

UserAccounts(UserID,用户名)
权限(UserID、PrivilegeType、读取、写入、修改、删除)

如何将 InvoicePrivielge 属性从用户帐户映射到 (UserID, 'Invoice') 权限。

我可以使用多对一来完成此操作,但我不需要一组权限,我宁愿将其映射到其属性。

I am creating the security system for the software im creating and I would like to know how i can do the following in NHibernate(if at all possible)

User Account Class:

public class UserAccount
{
    public virtual int UserID { get; set; }
    public virtual String Username { get; set; }
    public virtual Privilege InvoicePrivilege { get; set; }
    public virtual Privilege TradePrivilege { get; set; }
}

Privilege Class:

public class Privilege
{
    public virtual bool Read { get; set; }
    public virtual bool Write { get; set; }
    public virtual bool Delete { get; set; }
    public virtual bool Modify { get; set; }
}

I will be having a large number of these privilege objects (one for each traded entity) so in the database i have the following tables:

UserAccounts ( UserID, Username)
Privileges (UserID, PrivilegeType, Read,Write,Modify,Delete)

How can I map the InvoicePrivielge property from the user account to the (UserID, 'Invoice') Privilege.

I could do this using many-to-one but I don't want a collection of privileges, i'd rather map it to its property.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫离伤花 2024-09-08 01:55:15

我认为实现此目的的一种方法是创建 Privilege 类的两个子类:

public class InvoicePrivilege : Privilege 
{
}
public class TradePrivilege : Privilege
{
}

并设置鉴别器列和值。在流畅的映射中:

public class PrivilegeMap : ClassMap<Privilege>
{
  public PrivilegeMap()
  { 
     // ...
     DiscriminateSubClassesOnColumn("PrivilegeType")
       .SubClass<InvoicePrivilege>("Invoice", x => x.Map( ... ))
       .SubClass<TradePrivilege>("Trade", x => x.Map( ...));
  }
}

并使用 UserAccount 中的子类

public virtual InvoicePrivilege InvoicePrivilege { get; set; }
public virtual TradePrivilege TradePrivilege { get; set; }

I think a way to do this is to create two subclasses of the Privilege class:

public class InvoicePrivilege : Privilege 
{
}
public class TradePrivilege : Privilege
{
}

and set the discriminator column and value. In fluent mapping:

public class PrivilegeMap : ClassMap<Privilege>
{
  public PrivilegeMap()
  { 
     // ...
     DiscriminateSubClassesOnColumn("PrivilegeType")
       .SubClass<InvoicePrivilege>("Invoice", x => x.Map( ... ))
       .SubClass<TradePrivilege>("Trade", x => x.Map( ...));
  }
}

and use the subclasses in the UserAccount

public virtual InvoicePrivilege InvoicePrivilege { get; set; }
public virtual TradePrivilege TradePrivilege { get; set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文