Fluent NHibernate HasMany 其中 source 是逗号分隔的 Id 字符串

发布于 2024-11-03 00:37:55 字数 802 浏览 0 评论 0原文

我试图在尝试进行这种映射时理解流畅的 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 技术交流群。

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

发布评论

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

评论(1

紅太極 2024-11-10 00:37:55

我不认为你能做到你所要求的。我认为最好的选择是按照数据库中显示的方式映射它,并在映射之外处理 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.

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