如何在 Fluent NHibernate 中映射来自另一个 table 的字符串集合?
How to map a string collection from another table in Fluent NHibernate?
我有一个实体:
public class Foo
{
public virtual int Id;
public virtual IEnumberable<string> Bars;
}
及其映射:
public class FooMapping : ClassMap<Foo>
{
public FooMapping()
{
Table("foo_table_in_database");
Id(x => x.Id, "Id");
HasMany(x => x.Bars)
.AsList()
.Table("bars_table_in_db")
.Element("BarId", m =>
{
m.Type<string>();
});
}
}
并且实体内部返回了一个异常而不是预期结果:(
base = {"无法初始化集合:[Loya.Services.CouponsWeb.Promotion.LoyCouponCustomerGroups#2][SQL: SELECT loycouponc0_.Promotion_id as Promotion3_0_, loycouponc0_.LoyCustomerGroupId 作为 LoyCusto1_0_, loycouponc0_.Index 作为 Index0_ FROM loy_promotion__cu...
数据库表:
foo_table : *Id, 其他属性
bar_table : *FooId, *BarId
我的目标是在我的 Foo 中获取 BarId 的(字符串)列表。
如何正确映射它?
我认为您可能需要指定 KeyColumn
。我在我的一个解决方案中做了类似的事情,我将按如下方式映射上面的实体...
mapping.HasMany(x => x.Bars)
.Schema("Schema")
.Table("FooBars")
.Element("Bar")
.KeyColumn("FooID")
.ForeignKeyConstraintName("FK_FooBar_Foo")
.Not.Inverse()
.Cascade.All()
.Fetch.Select();
这将给出一个名为 Schema.FooBars
的 table。它将有一个 FooID
列(这是一个返回 Foo
table 的外键)和一个 Bar
列,其中包含您的 Bars 集合中的值。
我有一个实体:
public class Foo
{
public virtual int Id;
public virtual IEnumberable<string> Bars;
}
及其映射:
public class FooMapping : ClassMap<Foo>
{
public FooMapping()
{
Table("foo_table_in_database");
Id(x => x.Id, "Id");
HasMany(x => x.Bars)
.AsList()
.Table("bars_table_in_db")
.Element("BarId", m =>
{
m.Type<string>();
});
}
}
并且实体内部返回了一个异常而不是预期结果:(
base = {"无法初始化集合:[Loya.Services.CouponsWeb.Promotion.LoyCouponCustomerGroups#2][SQL: SELECT loycouponc0_.Promotion_id as Promotion3_0_, loycouponc0_.LoyCustomerGroupId 作为 LoyCusto1_0_, loycouponc0_.Index 作为 Index0_ FROM loy_promotion__cu...
数据库表:
foo_table : *Id, 其他属性
bar_table : *FooId, *BarId
我的目标是在我的 Foo 中获取 BarId 的(字符串)列表。 如何正确映射它?
我认为您可能需要指定 KeyColumn
。我在我的一个解决方案中做了类似的事情,我将按如下方式映射上面的实体...
mapping.HasMany(x => x.Bars)
.Schema("Schema")
.Table("FooBars")
.Element("Bar")
.KeyColumn("FooID")
.ForeignKeyConstraintName("FK_FooBar_Foo")
.Not.Inverse()
.Cascade.All()
.Fetch.Select();
这将给出一个名为 Schema.FooBars
的 table。它将有一个 FooID
列(这是一个返回 Foo
table 的外键)和一个 Bar
列,其中包含您的 Bars 集合中的值。