简体   繁体   English

实体框架核心 ForeignKeyAttribute 两边

[英]Entity Framework Core ForeignKeyAttribute on both sides

I have an application with Entity Framework Core with different entities in my context, but there are two of them that require a one-to-zero-or-one relation.我有一个带有 Entity Framework Core 的应用程序,在我的上下文中具有不同的实体,但是其中有两个需要一对零或一的关系。 My way of doing that and ensuring validation was to do something like the following:我这样做并确保验证的方法是执行以下操作:

public class PurchasedPackage
{
    public int Id { get; set; }

    ...

    [ForeignKey(nameof(PurchasedPackageModifier))]
    public int? PurchasedPackageModifierId { get; set; }

    public virtual PurchasedPackageModifier PurchasedPackageModifier { get; set; }
}

And

public class PurchasedPackageModifier
{
    public int Id { get; set; }

    ...

    [Required]
    [ForeignKey(nameof(PurchasedPackage))]
    public int PurchasedPackageId { get; set; }

    public virtual PurchasedPackage PurchasedPackage { get; set; }

    ...
}

Where PurchasedPackage is required and PurchasedPackageModifier is optional.其中PurchasedPackage是必需的, PurchasedPackageModifier是可选的。 By specifying ForeignKey attribute on both sides I'm trying to ensure data integrity across saves to both tables so nobody creates a PurchasedPackage with a non-existent PurchasedPackageModifier and vice versa.通过在双方指定ForeignKey属性,我试图确保保存到两个表的数据完整性,因此没有人创建带有不存在的PurchasedPackagePurchasedPackageModifier ,反之亦然。

While this works as required I'm getting warnings in my logs stating the following:虽然这可以按要求工作,但我在日志中收到警告,说明如下:

'PurchasedPackage.PurchasedPackageModifier' and 'PurchasedPackageModifier.PurchasedPackage' were separated into two relationships as ForeignKeyAttribute was specified on properties 'PurchasedPackageModifierId' and 'PurchasedPackageId' on both sides.

And

'PurchasedPackageModifier.PurchasedPackage' and 'PurchasedPackage.PurchasedPackageModifier' were separated into two relationships as ForeignKeyAttribute was specified on properties 'PurchasedPackageId' and 'PurchasedPackageModifierId' on both sides.

My question is: am I doing this right and should I ignore these warnings in this particular case or there is a better way of achieving same validation/integrity behavior?我的问题是:我这样做是否正确,在这种特殊情况下我应该忽略这些警告还是有更好的方法来实现相同的验证/完整性行为?

This issue about warnings is closed in April 2018. I did try your two classes now with mysql(pomelo lib) and indeed I get one FK.这个关于警告的问题已于 2018 年 4 月关闭。我现在确实用 mysql(pomelo lib) 尝试了你的两个课程,确实我得到了一个 FK。 So if this is acceptable to you you can ignore the warnings (to quote one of the devs from that github link)因此,如果您可以接受这些警告,则可以忽略警告(引用该 github 链接中的开发人员之一)

If you really want the "one-to-zero-or-one relation", then the following approach is how I'd do it:如果您真的想要“一对零或一的关系”,那么我将采用以下方法:

  • PurchasedPackage has no FK to PurchasedPackageModifier , by this I mean remove both int? PurchasedPackageModifierId PurchasedPackagePurchasedPackageModifier没有 FK,我的意思是删除两个int? PurchasedPackageModifierId int? PurchasedPackageModifierId and the navigation property urchasedPackageModifier PurchasedPackageModifier - this covers 1-0 part where a record in PurchasedPackage can exist without corresponding PurchasedPackageModifier existing int? PurchasedPackageModifierId和导航属性urchasedPackageModifier PurchasedPackageModifier - 这涵盖了 1-0 部分,其中PurchasedPackage中的记录可以存在而不存在相应的PurchasedPackageModifier
  • PurchasedPackageModifier has a column PurchasedPackageId which is FK to PurchasedPackage and also an unique index - this covers 1-1 part where PurchasedPackageModifier有一个PurchasedPackage列,它是PurchasedPackageId的 FK,也是一个唯一索引 - 这涵盖了 1-1 部分,其中

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM