[エンティティ・フレームワーク・コア] Entity Frameworkのコア一対一の関係を

https://www.learnentityframeworkcore.com/configuration/one-to-one-relationship-configuration

慣例により

    public class Author
    {
        public int AuthorId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public AuthorBiography Biography { get; set; }
    }
    public class AuthorBiography
    {
        public int AuthorBiographyId { get; set; }
        public string Biography { get; set; }
        public DateTime DateOfBirth { get; set; }
        public string PlaceOfBirth { get; set; }
        public string Nationality { get; set; }
        public int AuthorId { get; set; }
        public Author Author { get; set; }
    }

、移行のエラーを実行EF差原理エンティティへのFKによるコアとは、エンティティを依存していますが、出し入れしやすい大会で区別できないentitysを設計するとき(この時点では、多くの理由であってもよいです)。

子/依存側は、「<entity1.property2>」と「<entity2.property1>」との間に検出された一対一の関係を決定することができませんでした。関係の子/依存側を識別するために、外部キープロパティを設定します。

したがって、この時点で、あなたは流暢なAPIを解決するために使用する必要があります

流暢なAPIによって

    public class Author
    {
        public int AuthorId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public AuthorBiography Biography { get; set; }
    }
    public class AuthorBiography
    {
        public int AuthorBiographyId { get; set; }
        public string Biography { get; set; }
        public DateTime DateOfBirth { get; set; }
        public string PlaceOfBirth { get; set; }
        public string Nationality { get; set; }
        public int AuthorRef { get; set; }
        public Author Author { get; set; }
    }

流暢なAPIを使用しました:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Author>()
            .HasOne(a => a.Biography)
            .WithOne(b => b.Author)
            .HasForeignKey<AuthorBiography>(b => b.AuthorRef);
    }

2は、実際に、彼らは主キーを共有するテーブルの実際のエンティティをマッピングすることができますので、呼ば流暢APIによって達成され、1人の関係-に-また実際、著者と著者の伝記における設計の観点から、それは1つであるため。table splittingテクノロジー:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Author>()
            .HasOne(a => a.AuthorBiography).WithOne(b => b.Author)
            .HasForeignKey<AuthorBiography>(e => e.AuthorId);
        modelBuilder.Entity<Author>().ToTable("Authors");
        modelBuilder.Entity<AuthorBiography>().ToTable("Authors"); 
    }

おすすめ

転載: www.cnblogs.com/it-dennis/p/11654211.html