2016-12-21 5 views
0

私は2つのエンテインを持っています、プロスペクトと人は、私がしようとしているのはProspectTableのプライマリキーとして、そしてPersonIDの外部キーとして、 Ideaは、私のProspectエンティティでPersonIDを必要とせずに、両方のエンティティに同じIDを使用しています。見込み客がデータベースに保存されているとき、Prospectエンティティにこのプロパティがない場合でもPersonIDを保存しようとしますが、ef coreがこの種の関係をサポートしているかどうかを知りたいと思います。EFコア:同時にプライマリキーと外部キーとしてIDを使用

私のモデルビルダーは次のとおりです。 、

INSERT INTO [Prospects] ([ID], [PersonID]) VALUES (@p421, @p422)

PersonDTO:

modelBuilder.Entity<ProspectDto>(builder => { builder.ToTable("Prospects"); builder.HasKey(prospect => prospect.ID); }); 

modelBuilder.Entity<PersonDto>(builder => { builder.HasOne(p => p.Prospect).WithOne().HasForeignKey<ProspectDto>(pe => pe.ID); }); 

は、ここでデータベース上で実行されているものです

public class PersonDto : DtoBase 
{ 
    public PersonDto() 
    { 

    } 

    public ProspectDto Prospect { get; set; } 
} 

ProspectDTO:

public class ProspectDto : DtoBase 
{ 
    public ProspectDto() 
    { 

    } 

    public PersonDto Person { get; set; } = new PersonDto(); 
} 

DtoBase:

public abstract class DtoBase 
{ 
    public Guid ID { get; protected set; } 
} 

ありがとう。

+0

'ProspectDto'と' PersonDto'クラスの宣言を表示してください(あなたは質問に関係ないデータフィールドをスキップすることができます)。 – Dmitry

+0

@Dmitry更新されました –

+0

うーん、 'ID'フィールドは見えません...' DtoBase'に存在するかもしれないと思います...あなたのモデルに関するすべての情報を質問に関連させてくれますか? – Dmitry

答えて

1

public abstract class DtoBase 
{ 
    [Key] 
    public Guid ID { get; protected set; } 
} 

public class PersonDto : DtoBase 
{ 
    [InverseProperty("Person")] 
    public ProspectDto Prospect { get; set; } 
} 

public class ProspectDto : DtoBase 
{ 
    [ForeignKey("ID")]   // "magic" is here 
    public PersonDto Person { get; set; } = new PersonDto(); 
} 

を私はFluentAPIでForeignKeyと同等であるかわかりません。他のすべての(KeyとInverseProperty)は設定可能ですが、代わりに2つの方法を使用するのはなぜですか。

上記のコードは、次の移行コードを生成します。

protected override void Up(MigrationBuilder migrationBuilder) 
{ 
    migrationBuilder.CreateTable(
     name: "Persons", 
     columns: table => new 
     { 
      ID = table.Column<Guid>(nullable: false) 
     }, 
     constraints: table => 
     { 
      table.PrimaryKey("PK_Persons", x => x.ID); 
     }); 

    migrationBuilder.CreateTable(
     name: "Prospects", 
     columns: table => new 
     { 
      ID = table.Column<Guid>(nullable: false) 
     }, 
     constraints: table => 
     { 
      table.PrimaryKey("PK_Prospects", x => x.ID); 
      table.ForeignKey(
       name: "FK_Prospects_Persons_ID", 
       column: x => x.ID, 
       principalTable: "Persons", 
       principalColumn: "ID", 
       onDelete: ReferentialAction.Cascade); 
     }); 
} 

はあなたが必要なものに非常に近い見えます。

+0

これは完璧に動作します、ありがとう。その他の質問、流暢なAPIを使ってこれと同等のことを知っていますか? –

-1

関係を1つにモデル化すると、EFは自動的にプリンシパルのPKを依存関係のFKとして使用します。

ModelBuilder.Entity<ProspectDto>().HasRequired(p => p.Person).WithRequiredDependent(); 

ProspectDtoはまだ(DtoBaseから継承)DB上ID列を持つことになりますが、FK関係はProspectDto.IDPersonDto.IDの間になりますし、何のProspectDto.PersonId列があってはならないことに注意してください。 FluentAPIせずに、属性のみを使用して

+0

efコアでHasRequired()を使用できません。 –

+0

ご迷惑をおかけして申し訳ございませんが、このコードはEF 6.1プロジェクトのものです。 –

関連する問題