0

我有一个 Employee 实体,它有多个 Unit 实体,它们也以各种方式关联(例如,作为经理、主管等)。它们与 Unit 的关系在 UnitRelationships 表中定义,该表包括 IsActive 属性以及 EmployeeId、UnitId 和 RelationshipTypeId 属性。

我想做的是在 Employee 实体上创建特定属性,如下所示:

SupervisedUnits {get;set;}
ManagedUnits{get;set;}

...并配置映射,以便“SupervisedUnits”仅返回存在活动 (IsActive = true) 关系且 RelationshipTypeId='Supervisor' 的单元。与 ManagedUnits 相同类型的东西。

那么,有没有办法在 EF Code First 中执行此操作,以便我可以在我的 LINQ-to-Entities 查询中实际使用这些属性(SupervisedUnits 和 ManagedUnits)?

谢谢

4

1 回答 1

0

There are probably a couple of ways of doing this.

  1. The "Active" relationship is a one to many between Unit and Employee and the "Inactive" relationship is many-to-many. So set them up as two different relationships and when the relationship is made inactive, move the Supervisor into the InactiveSupervisors. Doing it this way gives you a SupervisedUnits navigation property (as you wanted) and an InActiveSupervisedUnits property

  2. The relationship is many-to-many and has attributes such as IsActive, ActiveFrom and ActiveTo. Now the relationship really needs to be an entity. You could use inheritance on your UnitRelationship entity:

    public class SupervisorUnitRelationShip : UnitRelationship{ }
    

    public class ManagerUnitRelationShip : UnitRelationship { }

then you can add your navigation properties by type:

public virtual Collection<SupervisorUnitRelationship> SupervisedRelations {get;set;}
public virtual Collection<ManagerUnitRelationship> ManagedRelations {get;set;}

to get to the Units you'll have to go through the UnitRelationship

var SupervisedUnits = SupervisedRelations.Units;

于 2013-09-27T10:49:05.257 回答