Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果导航属性名称与属性类型不同,如何使用NHibernate多对一/一对多外键?_C#_.net_Nhibernate_Fluent Nhibernate - Fatal编程技术网

C# 如果导航属性名称与属性类型不同,如何使用NHibernate多对一/一对多外键?

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

我正在使用自动映射配置的NHibernate/FluentNhibernate,在某些关系的外键方面遇到了问题。尤其是导航属性名称与其指向的类型名称不同的那些:

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
映射吗?