Fluent NHibernate HasMany 其中 source 是逗号分隔的 Id 字符串
我试图在尝试进行这种映射时理解流畅的 nHibernate:
public class ClassA : Entity
{
public virtual string SomeField { get; set; }
}
public class ClassB : Entity
{
public virtual string ClassAIds { get; set; }
public virtual IList<ClassA> ClassAList { get; set; }
}
其中 Entity 基本上确保两个类都将 Id 字段作为其主数据库键。
数据库看起来像:
table ClassA:
int Id,
varchar(25) SomeField
table ClassB:
int Id,
varchar(50) ClassAIds
我正在尝试编写一些映射,以便 ClassB 对象将用 ClassA 实体填充到 ClassAList 中。是否有可能有类似的东西:
public void Override(AutoMapping<ClassB> mapping)
{
mapping.HasMany(x => x.ClassAIds.Split(new char[] { ',' }).Select(i => int.Parse(i)).ToList()).KeyColumn("Id");
}
我真的不明白如何传递目标类类型以及是否有可能实现这一点。
Im' trying to wrap my head around fluent nHibernate while trying to do this kind of mapping:
public class ClassA : Entity
{
public virtual string SomeField { get; set; }
}
public class ClassB : Entity
{
public virtual string ClassAIds { get; set; }
public virtual IList<ClassA> ClassAList { get; set; }
}
Where Entity basically ensures that both classes have Id field as their primary database key.
Database would look like:
table ClassA:
int Id,
varchar(25) SomeField
table ClassB:
int Id,
varchar(50) ClassAIds
I'm trying to write some map so that ClassB object would be populated with ClassA entities into ClassAList. Is it possible to have something similar to:
public void Override(AutoMapping<ClassB> mapping)
{
mapping.HasMany(x => x.ClassAIds.Split(new char[] { ',' }).Select(i => int.Parse(i)).ToList()).KeyColumn("Id");
}
I don't really understand how to pass a target class type and if it is possible to achieve this at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为你能做到你所要求的。我认为最好的选择是按照数据库中显示的方式映射它,并在映射之外处理 ClassAList 集合的加载。我会将您的自动映射设置为忽略该集合。从数据库的角度来看,这并不是处理关系的好方法。
I don't think you can do what you are asking. I think your best bet would be to map it as it appears in the database and handle the loading of that ClassAList collection outside of the mapping. I would set your automapping up to ignore the collection. This is just not really a good way to handle relationships from a database perspective.