Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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# 具有2列主键的表的外键(CompositeId)_C#_Nhibernate_Fluent Nhibernate_Foreign Keys_Primary Key - Fatal编程技术网

C# 具有2列主键的表的外键(CompositeId)

C# 具有2列主键的表的外键(CompositeId),c#,nhibernate,fluent-nhibernate,foreign-keys,primary-key,C#,Nhibernate,Fluent Nhibernate,Foreign Keys,Primary Key,我将主键定义如下: CompositeId() .KeyProperty(x => x.Id) .KeyProperty(x => x.Type); References(x => x.EntityWith2ColsPK); 我尝试了以下方法: CompositeId() .KeyProperty(x => x.Id) .KeyProperty(x => x.Type); References(x => x.EntityW

我将主键定义如下:

CompositeId()
    .KeyProperty(x => x.Id)
    .KeyProperty(x => x.Type);
References(x => x.EntityWith2ColsPK);
我尝试了以下方法:

CompositeId()
    .KeyProperty(x => x.Id)
    .KeyProperty(x => x.Type);
References(x => x.EntityWith2ColsPK);
失败原因如下:

外键(Fk_MyEntity_EntityWith2ColsPK:MyEntities[Fk_EntityWith2ColsPK])必须与引用的主键(EntityWith2ColsPK[Id,Type])具有相同的列数

如何使用另一个实体的2Colspk引用实体


更新:

我尝试了以下方法(根据AlfeG的评论):

但我收到了以下例外情况:

System.IndexOutOfRangeException:此SqlParameterCollection的索引8无效,计数为8

因为此实体的映射与EntityWith2ColsPK相同:

CompositeId()
    .KeyProperty(x => x.Id)
    .KeyProperty(x => ((ILocalizedEntity) x).Language);

救命啊

您可以使用类似的内容,因为您在
参考上根本不使用cascade

References(x => x.EntityWith2ColsPK)
    .Columns(new string[] { "ID", "TYPE" })
    .Not.Update()
    .Not.Insert();

这是一个重复的问题,所以你可以通过这个链接找到anser这并不能解决我的问题:(非常感谢你,我花了整个周末试图研究这个问题,但没有成功..我肯定我已经尝试了你给我的东西,同时改变了其他东西,但失败了..现在成功了:)再次感谢。您可能需要将数据库中的列名(如果可以)更改为id和类型以外的其他名称。例如,在一个名为
order
的表中,您将有一个名为
order\u id
的列。。。因为我想更新并插入“OrderID”,但不想再次映射类型(因为类型已经是CompositeId的一部分),所以它仍然没有解决。我尝试了以下方法:
引用(memberExpression).Columns(“CategoryId”).Nullable().Columns(“语言”).not.Update().not.Insert()但它适用于两列,而不是插入和更新
References(x => x.EntityWith2ColsPK)
    .Columns(new string[] { "ID", "TYPE" })
    .Not.Update()
    .Not.Insert();