使用Code Only In Entity Framework 4进行组合键映射 [英] Composite key mapping using Code Only In Entity Framework 4
问题描述
我有两个实体Master和Detail都有复合键(多个键组合了唯一键)。
I have two entity Master and Detail have composite key (multiple key combines unique key).
- Master // POCO Entiry
keyA // pk
KeyB // pk
KeyC // pk
Prop1
Prop2
--Master //POCO Entiry
keyA //pk
KeyB //pk
KeyC //pk
Prop1
Prop2
列表<详细信息>详细信息//在Master entiry中使用Navigatin属性
List<Detail> detail //Navigatin propperty in Master entiry
- 详细信息// POCO实体
KeyA
KeyB
KeyC
KeyD
Prop11
Prop12
--Detail //POCO Entity
KeyA
KeyB
KeyC
KeyD
Prop11
Prop12
Master master //详细信息中的导航属性
Master master // Navigation property in Detail entiry
公共类MasterConfiguration:EntityConfiguration< ActivityCode>
{&NBSP;&NBSP;&NBSP;
public MasterConfiguration()
{
//设置PK
财产(c => c.KeyA).IsIdentity();
财产(c => c.KeyB).IsIdentity();
财产(c => c.KeyC).IsIdentity();
public class MasterConfiguration : EntityConfiguration<ActivityCode>
{
public MasterConfiguration()
{
//Set PK
Property(c => c.KeyA).IsIdentity();
Property(c => c.KeyB).IsIdentity();
Property(c => c.KeyC).IsIdentity();
&NBSP;&NBSP;&NBSP;&NBSP; }
}
}
}
公共类DetialConfiguration:EntityConfiguration< ActivityCode>
{
public DetailConfiguration()
{
//设置PK
属性(c => c.KeyA).IsIdentity(); //来自主实体的fk为
属性(c => c.KeyB).IsIdentity(); //来自主实体的fk为
属性(c => c.KeyC).IsIdentity(); //来自主实体的fk为
&NBSP;财产(c => c.KeyD).IsIdentity(); //
public class DetialConfiguration : EntityConfiguration<ActivityCode>
{
public DetailConfiguration()
{
//Set PK
Property(c => c.KeyA).IsIdentity(); // as fk from Master entity
Property(c => c.KeyB).IsIdentity(); // as fk from Master entity
Property(c => c.KeyC).IsIdentity();// as fk from Master entity
Property(c => c.KeyD).IsIdentity();//
//建立Master和Detail之间的关系
关系(p => p.Master).FromProperty(c => c.Detials).HasConstraint((p,c)=> p.KeyA == c.KeyA& p.KeyB == c.KeyB & P.keyC == c.KeyC);
}
}
// Establish relationship between Master and Detail
Relationship(p => p.Master).FromProperty(c => c.Detials).HasConstraint((p, c) => p.KeyA == c.KeyA & p.KeyB == c.KeyB & P.keyC== c.KeyC);
}
}
无编译错误但在运行时会出现以下错误
No compilation Error But while running it give following Error
为此配置的约束"Detial"类型的导航属性"Master"无效。目标类型"主"的每个关键属性必须在约束中包含一次。
推荐答案
现在我不能在以下方式转换关系后得到错误信息。
Now i couldn't get above Error message after wrting relationship in following way.
关系(p => p.Master).FromProperty( c => c.Detials).HasConstraint((p,c)=> p.KeyA == c.KeyA);
$
关系(p => p.Master).FromProperty( c => c.Detials).HasConstraint((p,c)=> p.KeyB == c.KeyB);
$
Relationship(p => p.Master).FromProperty( c => c.Detials).HasConstraint((p,c)=> P.keyC == c.KeyC);
Relationship(p => p.Master).FromProperty(c => c.Detials).HasConstraint((p, c) => p.KeyA == c.KeyA );
Relationship(p => p.Master).FromProperty(c => c.Detials).HasConstraint((p, c) => p.KeyB == c.KeyB );
Relationship(p => p.Master).FromProperty(c => c.Detials).HasConstraint((p, c) => P.keyC== c.KeyC);
我查看了SQL profiler.But,它不在Where子句中应用这3个键。它仅适用于最后的关系( KeyC)。我不知道为什么它不起作用。帮助我如何在关系中使用多个属性映射(仅限代码功能)。
I looked at SQL profiler.But, It doesn't apply these 3 keys in Where clause. It applies only last relationship( KeyC). I don't know why it doesn't work. Help me how to use multiple property mapping in relationship(In Code Only feature).
这篇关于使用Code Only In Entity Framework 4进行组合键映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!