FluentNhbernate 映射问题

发布于 2024-08-14 17:46:18 字数 2941 浏览 4 评论 0原文

我对所有 OOP 和 ORM 内容都很陌生,所以我非常感谢您的帮助...

我有一个团队类:

public class Team : IEntity<Team>
{
    public virtual int ID { get; private set; }

    public virtual string Code{ get; private set; }

    public virtual TeamWorker TeamLeader { get; private set; }

    public virtual IEnumerable<TeamWorker> Workers {get; private set;}
    //etc

}

一个 Worker 类:

public class Worker: IEntity<Worker>
{
    public virtual int ID { get; private set; }

    public virtual string Name { get; set; }
    //etc
}

一个 Team Worker 类,它将 Worker “粘合”到总价值的一定百分比上给一个团队:

   public class TeamWorker
   {
        public virtual Worker Worker{ get; private set; }
        public virtual Percentage Comission{ get; private set; }
   }

我只需要公共十进制值属性的百分比类

到目前为止,我能够映射团队中的领导者:

public class TeamMap : ClassMap<Team>
{

    public TeamMap()
    {
        Table("Team");

        Id(e => e.ID, "ID").GeneratedBy.Identity();

        Map(e => e.Code, "Code").Unique().Not.Nullable();

        Component(team => team.Leader,
                  m =>
                  {
                      m.References(teamWorker => teamWorker.Worker, "IDTeamWorker").Cascade.SaveUpdate();
                      m.Component(teamWorker  => teamWorker.Commission,
                                  p =>
                                  p.Map(commission=> commission.Value, "LeaderCommission").
                                      CustomType(typeof(decimal)).Nullable());
                  }
            );

  }

}

这映射了团队表中的领导者和他的委员会,问题是我'我正在映射工人......

我尝试了这个:

    HasMany(team => team.Workers).Table("TeamWorkers").Component(m =>
                                            {
                                                m.References(twk => twk.Worker).Cascade.SaveUpdate();
                                                m.Map(twm => twk.Commission, "Commission");
                                            }
            );

但后来我得到了这个: 无法确定类型:百分比

发生这种情况是因为佣金是百分比,而我没有映射它,因为它是一个值对象,如果我能做到这一点:

 HasMany(team => team.Workers).Table("TeamWorkers").Component(m =>
                                            {
                                                m.References(twk => twk.Worker).Cascade.SaveUpdate();
                                                m.Component(twk => twk.Commission,
                                      m2 => m2.Map(commission=> commission.Value, "Comission").CustomType(typeof(decimal)).Nullable());
                                            }
                );

但组件在这种情况下不是一个选项...

我需要找到一种拥有

带有 TeamId WorkerId 和 Commission 的 TeamWorkers 表的

方法TeamID 和 WorkerID 是 Team 和 Worker 表中的外键,Commission 是 Percentage 类中的值属性。

I am new to all the OOP and ORM stuff, so i would appreciate your help...

I have a Team Class:

public class Team : IEntity<Team>
{
    public virtual int ID { get; private set; }

    public virtual string Code{ get; private set; }

    public virtual TeamWorker TeamLeader { get; private set; }

    public virtual IEnumerable<TeamWorker> Workers {get; private set;}
    //etc

}

A Worker Class:

public class Worker: IEntity<Worker>
{
    public virtual int ID { get; private set; }

    public virtual string Name { get; set; }
    //etc
}

A Team Worker Class, that "glues" a Worker to a percentage of the total value that will be given to a Team:

   public class TeamWorker
   {
        public virtual Worker Worker{ get; private set; }
        public virtual Percentage Comission{ get; private set; }
   }

And the Percentage class from wich i just need the public decimal Value property

So far i was able to map the Leader in the Team:

public class TeamMap : ClassMap<Team>
{

    public TeamMap()
    {
        Table("Team");

        Id(e => e.ID, "ID").GeneratedBy.Identity();

        Map(e => e.Code, "Code").Unique().Not.Nullable();

        Component(team => team.Leader,
                  m =>
                  {
                      m.References(teamWorker => teamWorker.Worker, "IDTeamWorker").Cascade.SaveUpdate();
                      m.Component(teamWorker  => teamWorker.Commission,
                                  p =>
                                  p.Map(commission=> commission.Value, "LeaderCommission").
                                      CustomType(typeof(decimal)).Nullable());
                  }
            );

  }

}

this maps the Leader and his commision in the team table, the problem i'm having is mapping the Workers...

i tried this:

    HasMany(team => team.Workers).Table("TeamWorkers").Component(m =>
                                            {
                                                m.References(twk => twk.Worker).Cascade.SaveUpdate();
                                                m.Map(twm => twk.Commission, "Commission");
                                            }
            );

But then i got this:
Could not determine type for: Percentage

this happens because Commission is a Percentage and i didn't map it because its a value object, if only i could do this:

 HasMany(team => team.Workers).Table("TeamWorkers").Component(m =>
                                            {
                                                m.References(twk => twk.Worker).Cascade.SaveUpdate();
                                                m.Component(twk => twk.Commission,
                                      m2 => m2.Map(commission=> commission.Value, "Comission").CustomType(typeof(decimal)).Nullable());
                                            }
                );

but Component its not an option in this context...

i need to find a way to have

TeamWorkers table with TeamId WorkerId and Commission

TeamID and WorkerID are foreign keys from Team and Worker table, and Commission is the value property in Percentage class.

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

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

发布评论

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

评论(1

尸血腥色 2024-08-21 17:46:18

不要使用Component,它用于值类型和处理规范化表。您需要为您的 WorkerTeamWorker 创建正确的映射,并将它们视为正确的实体。

创建 WorkerMapTeamWorker 地图,然后您可以更新 TeamMap 以仅使用 HasMany(x => x.Workers)

至于 Percentage,您需要使用它自己的 ClassMap 进行映射,或者在新的 TeamWorker 中使用 Component > 地图。

Don't use Component, that's for value types and dealing with normalized tables. You need to be creating proper mappings for your Worker and TeamWorker, and treat them as proper entities.

Create a WorkerMap and TeamWorker map, then you can update your TeamMap to just use HasMany(x => x.Workers).

As for Percentage, you either need to map that with it's own ClassMap, or use Component inside of your new TeamWorker map.

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