【问题标题】:Junction Tables: Changing many-to-many to one-to-many in Entity Framework model连接表:在实体框架模型中将多对多更改为一对多
【发布时间】:2011-05-11 21:31:12
【问题描述】:

我有一组表格如下:

PositionShiftPattern
PositionShiftPatternID
位置ID

EmployeePositionShiftPattern
EmployeePositionShiftPatternID
员工职位ID

模板转换模式
模板ShiftPatternID


周ID

PositionShiftPatternJunction
PositionShiftPatternID
周ID

EmployeePositionShiftPatternJunction
EmployeePositionShiftPatternID
周ID

TemplateShiftPatternJunction
模板ShiftPatternID
周ID

因此,PositionShiftPattern、EmployeePositionShiftPattern 和 TemplateShiftPattern 都有一个与 Week 的联结,它们之间是一对多的关系。

显然 EF 会将 PositionShiftPattern、EmployeePositionShiftPattern 和 TemplateShiftPattern 与 Week 之间的关系映射为多对多,并从每个表直接创建导航属性到 Week。

是否有可能将多对多关系更改为一对多关系(例如,每周应映射到单个 ShiftPattern)?我正在尝试实现互斥(见下图),并希望避免仅将 PositionShiftPatternID、EmployeePositionShiftPatternID 和 TemplateShiftPatternID 添加到 Week。我应该寻找另一种实现互斥的方法吗?

谢谢

【问题讨论】:

    标签: sql entity-framework


    【解决方案1】:

    不,这是不可能的。关系必须与数据库中的关系相同。如果您创建一对多关系,它将被视为一个新关系,您必须将这些 ID 添加到周。同样,在查看您的模型时,引入这种关系是没有意义的。目前 Shift 可能与许多周有关,而周可以有许多班次。如果引入一对多,则意味着每周实例只能有一个班次。

    【讨论】:

    • @Gib:我的回答仍然有效。您的数据库不是为互斥而建模的。您应该从您的数据库模式开始,因为它对您的要求没有意义,您必须将这些 id 添加到周表中以建立一对多关系。您还必须验证是否只填写了一个 id。
    • 好的,我打算将字段添加到 Week 并删除连接点。感谢您回答拉迪斯拉夫。
    猜你喜欢
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 2012-05-28
    • 1970-01-01
    相关资源
    最近更新 更多