C# 如果导航属性名称与属性类型不同,如何使用NHibernate多对一/一对多外键?
我正在使用自动映射配置的NHibernate/FluentNhibernate,在某些关系的外键方面遇到了问题。尤其是导航属性名称与其指向的类型名称不同的那些:C# 如果导航属性名称与属性类型不同,如何使用NHibernate多对一/一对多外键?,c#,.net,nhibernate,fluent-nhibernate,C#,.net,Nhibernate,Fluent Nhibernate,我正在使用自动映射配置的NHibernate/FluentNhibernate,在某些关系的外键方面遇到了问题。尤其是导航属性名称与其指向的类型名称不同的那些: public class Country { public virtual string Code { get; set; } public virtual string Name { get; set; } public virtual Currency DefaultCurrency { g
public class Country
{
public virtual string Code { get; set; }
public virtual string Name { get; set; }
public virtual Currency DefaultCurrency { get; set; }
}
public class Currency
{
public virtual string Code { get; set; }
public virtual decimal Rate { get; set; }
public virtual IList<Country> Countries { get; set; }
}
公共类国家
{
公共虚拟字符串代码{get;set;}
公共虚拟字符串名称{get;set;}
公共虚拟货币默认货币{get;set;}
}
公营货币
{
公共虚拟字符串代码{get;set;}
公共虚拟十进制速率{get;set;}
公共虚拟IList国家{get;set;}
}
对于导航属性DefaultCurrency
的名称与名称Currency
类型不同的国家/地区实体。NHibernate的自动映射将猜测Country表将具有以下外键:
:对应于DefaultCurrency\u id
Country.Currency的关系
:对应于Currency\u id
货币的关系。国家
Currency.Countries
可以用DefaultCurrency\u id
键表示,从而导致国家表只能使用一个键foreign:
:对应于DefaultCurrency\u id
和Country.Currency
Currency.Countries
References(x => x.Foo, "MyFooId")
因为有很多:
HasMany(x => x.Foos)
.KeyColumn("MyFooId")
对于多对多:
HasManyToMany(x => x.Foos)
.ChildKeyColumn("MyFooId")
.ParentKeyColumn("MyFooId")
您也可以使用约定,例如:
public class HasManyConventions : IHasManyConvention
{
public void Apply(IOneToManyCollectionInstance target)
{
target.Key.Column(target.EntityType.Name + "Id");
}
}
你能发布你的
Fluent
映射吗?