Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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# Fluent Nhibernate外键映射_C#_Foreign Keys_Fluent Nhibernate_Fluent Nhibernate Mapping - Fatal编程技术网

C# Fluent Nhibernate外键映射

C# Fluent Nhibernate外键映射,c#,foreign-keys,fluent-nhibernate,fluent-nhibernate-mapping,C#,Foreign Keys,Fluent Nhibernate,Fluent Nhibernate Mapping,我有以下表格: 玩家 Id:int-primarykey 名称:string BowlerType Id:int-primarykey 描述:字符串 播放机广播类型 PlayerId:int非空外键引用Player.Id BowlerTypeId:int非空外键引用BowlerType.Id 一个球员可以确认许多保龄球类型。下面是一些示例数据 玩家 1|彼得 2 |约翰 BowlerType 6 |慢 7 |快速 播放机广播类型 1|6 1|7 2 | 7这里您需要的是一个用于PlayerBow

我有以下表格:

玩家
Id:int-primarykey
名称:string

BowlerType
Id:int-primarykey
描述:字符串

播放机广播类型
PlayerId:int非空外键引用Player.Id
BowlerTypeId:int非空外键引用BowlerType.Id

一个球员可以确认许多保龄球类型。下面是一些示例数据

玩家
1|彼得
2 |约翰

BowlerType
6 |慢
7 |快速

播放机广播类型
1|6
1|7

2 | 7

这里您需要的是一个用于PlayerBowlerType的复合id。此设置应在以下情况下工作:

public class PlayerBowlerTypeId
{
    public virtual int PlayerId { get; set; }

    public virtual int BowlerTypeId { get; set; }

    public override bool Equals(object obj)
    {
        return Equals(obj as PlayerBowlerTypeId);
    }

    private bool Equals(PlayerBowlerTypeId other)
    {
        if (ReferenceEquals(other, null)) return false;
        if (ReferenceEquals(this, other)) return true;

        return PlayerId == other.PlayerId &&
            BowlerTypeId == other.BowlerTypeId;
    }

    public override int GetHashCode()
    {
        unchecked 
        {
            int hash = GetType().GetHashCode();
            hash = (hash * 31) ^ PlayerId.GetHashCode();
            hash = (hash * 31) ^ BowlerTypeId.GetHashCode();

            return hash;
        }
    }
}

public class PlayerBowlerType
{
    public PlayerBowlerType()
    {
        Id = new PlayerBowlerTypeId();
    }

    public virtual PlayerBowlerTypeId Id { get; set; }
}

public class PlayerBowlerTypeMap : ClassMap<PlayerBowlerType>
{
    public PlayerBowlerTypeMap()
    {
        Table("TABLENAME");

        CompositeId<PlayerBowlerTypeId>(x => x.Id)
            .KeyProperty(x => x.BowlerTypeId, "COLUMNNAME")
            .KeyProperty(x => x.PlayerId, "COLUMNNAME");
    }
}
公共类PlayerBowlerTypeId
{
公共虚拟整数PlayerId{get;set;}
公共虚拟int BowlerTypeId{get;set;}
公共覆盖布尔等于(对象对象对象)
{
返回等于(对象作为PlayerBowlerTypeId);
}
私有布尔等于(PlayerBowlerTypeId其他)
{
if(ReferenceEquals(other,null))返回false;
if(ReferenceEquals(this,other))返回true;
return PlayerId==other.PlayerId&&
BowlerTypeId==other.BowlerTypeId;
}
公共覆盖int GetHashCode()
{
未经检查
{
int hash=GetType().GetHashCode();
哈希=(哈希*31)^PlayerId.GetHashCode();
哈希=(哈希*31)^BowlerTypeId.GetHashCode();
返回散列;
}
}
}
公共类PlayerBowlerType
{
公共PlayerBowlerType()
{
Id=新的PlayerBowlerTypeId();
}
公共虚拟播放器BowlerTypeId{get;set;}
}
公共类PlayerBowlerTypeMap:ClassMap
{
公共PlayerBowlerTypeMap()
{
表(“表名”);
CompositeId(x=>x.Id)
.KeyProperty(x=>x.BowlerTypeId,“COLUMNNAME”)
.KeyProperty(x=>x.PlayerId,“COLUMNNAME”);
}
}
从技术上讲,您可以在不使用identity对象的情况下执行此操作(PlayerBowlerTypeId类型将被删除,代码将直接放入PlayerBowlerType中并进行适当调整),但这样做会导致许多问题(3-4个单独的bug)。讨论了其中的一个问题

虽然我不喜欢更改域对象来补偿ORM系统中的错误,但如果您只使用PlayerBowlerTypeId类型,它将为您省去很多麻烦


只要您修改映射以使用实际的表名和列名(以及您需要为特定设置对映射执行的任何其他操作),这应该可以正常工作。

我认为我们可以使用HasManytoMany。 根据您的需求,您必须创建一个包含球员和投球手类型ID的表。这是一种多对多的关系

如果你想看看这个网站: 商店和产品的映射与预期映射相同。