简体   繁体   English

具有复合外键的实体框架导航属性

[英]Entity Framework Navigation Property with Composite Foreign Key

EF 6.1.3. EF 6.1.3。

I have a domain which contains many instances of a "Header/ Item" type pattern, where the a Header can have many Items (1 to many), and also has a "current" or "latest" item. 我有一个域,其中包含“ Header / Item”类型模式的许多实例,其中一个Header可以具有很多项目(1到很多),并且还具有“当前”或“最新”项目。

This is represented as follows: 表示如下:

Header
    Guid Id
    Guid CurrentItemId
    Item CurrentItem 
    ICollection<Item> AllItems

Item
    HeaderId
    Id

The PK of the Items is always the HeaderID + ItemID. 项目的PK始终为HeaderID + ItemID。 The reason being that, by far, the most common access pattern for items is to list all items related to a given header, and having HeaderID be the first part of the PK/clustered index means we get that data with clustered index seeks. 原因是,到目前为止,最常见的项目访问方式是列出与给定标头相关的所有项目,并且将HeaderID作为PK /聚簇索引的第一部分意味着我们可以通过聚簇索引查找获得数据。

Our problem is that when we use the CurrentItem navigation property, it only ever uses the ItemID to do the lookup, which results in not so great query plans. 我们的问题是,当我们使用CurrentItem导航属性时,它仅使用ItemID进行查找,从而导致查询计划不那么理想。

I assume this is because the conventions for EF us to use the CurrentItemId to look up the CurrentItem. 我认为这是因为EF我们使用CurrentItemId来查找CurrentItem的约定。 My question is, is there a way for my to tell EF to always perform its joins for CurrentItem by mapping the Header.Id,Header.CurrentItemId -> Item.HeaderId,Item.Id? 我的问题是,有没有办法让我通过映射Header.Id,Header.CurrentItemId-> Item.HeaderId,Item.Id来告诉EF始终为CurrentItem执行其联接?

I believe this is a slight different scenario than the one described here: composite key as foreign key 我认为这与此处描述的场景略有不同: 复合键作为外键

In my case, I have a one to one mapping not one top many, and there doesn't seem to be a WithforeignKey method available for that scenario. 在我的情况下,我有一个一对一的映射,而不是一个多的映射,而且似乎没有适用于该场景的WithforeignKey方法。

We ended up not being able to get EF to generate the SQL the way we wanted - so we wrote a db command interceptor to dynamically find instances of this join and re-write the join to match our designed composite key. 我们最终无法获得EF以所需的方式生成SQL-因此我们编写了db命令拦截器来动态查找此联接的实例,然后重新编写联接以匹配我们设计的组合键。

We configure this as the DbContext level like so: 我们将其配置为DbContext级别,如下所示:

    this.ModifyJoin<Item, Header>(
        (i) => new Header() { CurrentItemId = i.Id }, //What to find
        (i) => new Header() { CurerntItemId = i.Id, Id = i.HeaderId }); //What to replace with

This information is attached to the context instance itself, so when the command interceptor sees the overrides, it uses them to re-write the SQL. 此信息附加到上下文实例本身,因此,当命令拦截器看到替代时,它将使用它们来重写SQL。

This ends up working well for most scenarios, but there are some - such as when additional filtering is doing on the Item table as part of the LINQ statement, that the aliasing rules used by EF become too complex to follow without writing a full SQL parser. 在大多数情况下,这最终都能很好地工作,但是有一些情况-例如当作为LINQ语句的一部分对Item表进行额外的过滤时,EF使用的别名规则变得太复杂而无法编写完整的SQL解析器而无法遵循。

For our use, this results in the ideal join about 90% of the time, which is good enough for us. 对于我们的使用,这导致大约90%的时间有理想的连接,这对我们来说已经足够了。

The code to do all this isn't difficult, but it's too big to put here. 完成所有这些操作的代码并不困难,但是太大了,无法放在这里。 Add a comment if you want a copy and I'll put it up on GitHub. 如果您想要复制,请添加评论,我将其放在GitHub上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架:外键作为组合键的一部分 - Entity Framework:Foreign Key as part of Composite key 实体框架,更改外键,但导航属性不变 - entity framework, change foreign key but navigation property not changing 没有导航属性的外键实体框架数据注释 - Entity framework data annotation of foreign key without a navigation property 在实体框架中将表单导航属性迁移到外键 - Migrating form Navigation Property to Foreign Key in Entity Framework 实体框架可以将同一列映射到导航属性和外键吗 - Can Entity Framework map the same column to a Navigation Property and a Foreign Key 实体框架 - 未设置外键(0 / null)但导航属性不为空 - Entity Framework - Foreign Key not set (0 / null) but navigation property is not null 使用UserName作为实体框架中的外键创建ApplicationUser导航属性 - Creating an ApplicationUser navigation property using UserName as the foreign key in Entity Framework 如何在没有反向导航属性的实体框架中配置外键? - How to configure foreign key in Entity Framework without an inverse navigation property? 外键映射到实体框架中的复合键 - Foreign Key mapping to composite keys in entity framework 实体框架6代码优先,复合外键 - Entity Framework 6 Code First, Composite Foreign Key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM