FluentNhbernate 映射问题
我对所有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用
Component
,它用于值类型和处理规范化表。您需要为您的Worker
和TeamWorker
创建正确的映射,并将它们视为正确的实体。创建
WorkerMap
和TeamWorker
地图,然后您可以更新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 yourWorker
andTeamWorker
, and treat them as proper entities.Create a
WorkerMap
andTeamWorker
map, then you can update yourTeamMap
to just useHasMany(x => x.Workers)
.As for
Percentage
, you either need to map that with it's ownClassMap
, or useComponent
inside of your newTeamWorker
map.