如何在不实现 NHibernate.Collection 的情况下使用 Fluent NHibernate 映射实现 IEnumerable 的自定义集合?
我们使用自定义集合类来保存实体集合,但我在映射 Fluent NHibernate 的集合属性时遇到问题。我已阅读该网站上有关类似主题的所有帖子,但似乎没有一个帖子能够解决我们的特殊情况。
我们模型的典型集合类是:
public class ElementList : IEnumerable<Element>
{
private List<Element> list = new List<Element>();
public ElementList(IEnumerable<Element> elements)
{
this.list.AddRange(elements);
}
protected ElementList() {}
protected List<Element> InternalList
{
get { return this.list; }
set { this.list = value; }
}
public IEnumerator<Element> GetEnumerator()
{
return this.list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
这是父对象上的属性:
public class Paragraph
{
private ElementList elements;
public Paragraph(params Element[] elements)
{
this.elements = new ElementList(elements.ToList());
}
protected Paragraph()
{
}
public virtual int Id { get; protected set; }
public virtual ElementList Elements
{
get { return this.elements; }
protected set { this.elements = value; }
}
}
我尝试按如下方式映射它:
public ParagraphMap()
{
this.Id(x => x.Id);
Component(
x => x.Elements,
m => m.HasMany<Element>(Reveal.Member<ElementList>("InternalList")).AsSet().KeyColumn("ParagraphId"));
}
当我创建存储库并且按照我的预期创建表时,这很好。问题是当我尝试添加 Paragraph 对象时。以下代码抛出 InvalidCastException,因为我无法将类型为“NHibernate.Collection.Generic.PersistentGenericSet[Element]”的对象转换为类型“System.Collections.Generic.List[Element]”:
var elements = new Element[] { new Element(), new Element() };
var paragraph = new Paragraph(elements);
using (var unitOfWork = this.repository.Begin())
{
this.repository.Add(paragraph);
}
我做错了什么?
我注意到很多帖子说最好不要使用自定义集合,但这样做是我们项目的标准,所以我想坚持下去并让它发挥作用。
We are using custom collection classes to hold our entity collections but I am having a problem mapping the collection properties for Fluent NHibernate. I have read all the posts on this site on a similar theme but none seem to address our particular situation.
A collection class typical of our model is:
public class ElementList : IEnumerable<Element>
{
private List<Element> list = new List<Element>();
public ElementList(IEnumerable<Element> elements)
{
this.list.AddRange(elements);
}
protected ElementList() {}
protected List<Element> InternalList
{
get { return this.list; }
set { this.list = value; }
}
public IEnumerator<Element> GetEnumerator()
{
return this.list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
This is a property on the parent object:
public class Paragraph
{
private ElementList elements;
public Paragraph(params Element[] elements)
{
this.elements = new ElementList(elements.ToList());
}
protected Paragraph()
{
}
public virtual int Id { get; protected set; }
public virtual ElementList Elements
{
get { return this.elements; }
protected set { this.elements = value; }
}
}
I have tried mapping this as follows:
public ParagraphMap()
{
this.Id(x => x.Id);
Component(
x => x.Elements,
m => m.HasMany<Element>(Reveal.Member<ElementList>("InternalList")).AsSet().KeyColumn("ParagraphId"));
}
This is fine when I create my repository and the tables are created as I'd expect. The problem is when I try and add a Paragraph object. The following code throws an InvalidCastException because I am unable to cast an object of type 'NHibernate.Collection.Generic.PersistentGenericSet[Element]' to type 'System.Collections.Generic.List[Element]':
var elements = new Element[] { new Element(), new Element() };
var paragraph = new Paragraph(elements);
using (var unitOfWork = this.repository.Begin())
{
this.repository.Add(paragraph);
}
What am I doing wrong?
I have noted a lot of posts saying it is perhaps better not to use custom collections but doing so is a standard on our project so I would like to persevere and get it working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的自定义集合还需要实现
IList
或ICollection
。可能是 ICollection,因为它是一个集合。I think your custom collection also needs to implement either
IList<Element>
orICollection<Element>
. Probably ICollection as it is a set.