gpt4 book ai didi

c# - 映射实体无法完成,因为它找不到属性

转载 作者:太空宇宙 更新时间:2023-11-03 13:35:30 24 4
gpt4 key购买 nike

我有具有关键字和关键字属性的页面实体。

Keyword 属性存储一个字符串,如 string Keyword = "my awesome page, page";

关键字会将此信息存储在数据库中。

另一方面,关键字检索此关键字内容并以逗号分隔。

所以我有我的模型

public Page : Entity<int>
{
public virtual string Keyword {get; set;}
public virtual IList<string> Keywords
{
get { return Keyword.Split(','); }
set { Keyword = string.Join(",", value); }
}
public Page() { Keywords = new List<string>(); }
}

所以我尝试通过代码将这个实体映射为一致性映射

public class PageMap : ClassMapping<Page>
{
public PageMap()
{
Property(x => x.Keyword);
Property(x => x.Keywords, m =>
{
m.Access(Accessor.Field);
});
}
}

但是我得到了类似的东西

NHibernate.MappingException : Could not compile the mapping document: mapping_by_code ----> NHibernate.MappingException : Problem trying to set property type by reflection NHibernate.PropertyNotFoundException : Could not find property nor field 'Keywords' in class 'Model.Page'

即使我理解无法找到属性的消息,我也不明白为什么?以及如何克服这一点。

谢谢

最佳答案

有两个问题。首先,Keywords 属性是“虚拟”的,这意味着它不是持久化的,而是在运行时计算/构建的。要使其工作,您不需要映射,因此不需要存储它。只需删除关键字映射...

第二件事是映射。以防万一Keywords不会是虚拟的(运行时根据 Keyword 属性值解析),我们必须将其映射为集合。任何集合(例如 IList<string> )表示一对多关系,(流利地称为 HasMany):

<bag name="Keywords" table="Keywords">
<key column="PageId" />
<element column="Keyword" />
</bag>

这会将所有关键词存储在它自己的表中(例如 Keywords),由 PageId 引用列并保留在名为 nvrachar 的列(例如 Keyword) 中. Fluent 语法如下:

HasMany(x => x.Keywords)
.Table("Keywords")
.KeyColumn("PageId")
.Element("Keyword");

但是,如前所述:如果 Keywords是虚拟的,不要映射它。

关于c# - 映射实体无法完成,因为它找不到属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18848803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com