【问题标题】:EF Core - Self referencing relationship with custom foreign/principal key not mapped propertiesEF Core - 与自定义外键/主键未映射属性的自引用关系
【发布时间】:2020-05-03 11:36:04
【问题描述】:

我正在尝试在 EF Core 中实现自引用关系。 我正在尝试使用未映射到真实数据库列的自定义外键和主键。

使用下面的代码,我得到了错误

'找不到实体类型'Person'的属性'HeadOfHouseholdForeignKey'的支持字段,并且该属性没有设置器

有没有办法让这些外键/主键没有映射到数据库中的列? 有没有办法在 EF Core 中做得很好?

例子:

PersonContext.cs

using Microsoft.EntityFrameworkCore;

namespace ConsoleApp1
{
    public class PersonContext : DbContext
    {
        DbSet<Person> People;
        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<Person>()
                .HasOne(p => p.HeadOfHouseHold)
                .WithMany()
                .HasForeignKey(p => p.HeadOfHouseholdForeignKey)
                .HasPrincipalKey(p => p.HeadOfHouseholdPrincipalKey);
        }
    }
}

Person.cs

namespace ConsoleApp1
{
    internal class Person
    {
        public int PersonId { get; set; }
        public string Name { get; set; }

        public string FamilyName { get; set; }

        public string PersonSequenceNumber { get; set; }

        public Person HeadOfHouseHold { get; set; }

        public string HeadOfHouseholdForeignKey
        {
            get
            {
                // Head of household always has '01' as PersonSequenceNumber in DB, other members increment from 01 to 02, 03, etc
                if (PersonSequenceNumber != "01")
                {
                    return FamilyName + "01";
                }
                else
                {
                    return null;
                }
            }
        }

        public string HeadOfHouseholdPrincipalKey
        {
            get
            {
                return FamilyName + PersonSequenceNumber;

            }
        }
    }
}

【问题讨论】:

  • 外键是一个数据库概念。你想通过只有“外键”的代码模型来实现什么?
  • 我希望能够使用 EF Core .Include() 方法来简化获取相关数据的过程
  • 你想要什么表。Include()?也许是人?
  • 相关表的键是什么样的?它们像“Tom01”吗?
  • 类似People.Include(p =&gt; p.HeadOfHousehold)。我想使用复合键 { FamilyName, PersonSequenceNumber } 包含来自同一个表的记录(因此是自引用的)。例如,Jones01 是爸爸,Jones02 是妈妈,等等

标签: c# entity-framework entity-framework-core


【解决方案1】:

在 EF Core 中不可能有关系,它不是使用下面的数据库定义的。您正在尝试通过使用只读属性来使用外键,但不支持此操作。

如果你只是想 .Include() Person 表,你也许可以使用复合键:

.HasForeignKey(p => new { p.FamilyName, p.PersonSequenceNumber) });

查看示例:EF Core Relations #Foreign keyEF Core Relations #Principal key

PS。我想主键应该来自引用的表,而不是当前的:

.HasPrincipalKey(parent => parent.HeadOfHouseholdPrincipalKey);

【讨论】:

    【解决方案2】:

    创建模型时,您需要准确映射数据库中的内容。 因此,如果我正确理解您要执行的操作,您需要做两件事:

    1. 为不在数据库中的列删除这些声明。您需要数据库中的主键,这是您在创建模型时唯一需要声明的内容:

      // Remove these as these fields are not in the DB
      .HasForeignKey(p => p.HeadOfHouseholdForeignKey)
      .HasPrincipalKey(p => p.HeadOfHouseholdPrincipalKey);
      
    2. NotMapped属性添加到数据库中没有列的属性如下:

      [NotMapped]
      public string HeadOfHouseholdForeignKey
      // ...
      
      [NotMapped]
      public string HeadOfHouseholdPrincipalKey
      // ...
      

    【讨论】:

      猜你喜欢
      • 2019-10-03
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      • 2021-12-12
      • 2018-12-28
      • 2013-06-23
      相关资源
      最近更新 更多