0

私のテーブルは次のとおりです。

create table Entities(
    EntityId bigint not null identity(1, 1),
    Name nvarchar(64) not null,
    ParentEntityId bigint null
)
alter table Entities add constraint PK primary key (EntityId)
alter table Entities add constraint FK foreign key (ParentEntityId) references Entities(EntityId)

私のモデルは次のようになります。

public class Entity
{
    [Required]
    public virtual long EntityId { get; set; }

    [Required]
    public virtual string Name { get; set; }

    public virtual long? ParentEntityId { get; set; }

    public virtual Entity ParentEntity { get; set; }
}

流暢なAPIマッピングを使用してプロパティをマッピングしようとしていParentEntityますが、これを機能させることができませんでした。どうすればいいですか?
前もって感謝します!

編集:コードの不一致を修正しました。

4

1 に答える 1

2

この流暢さはあなたのために働くでしょう:

        modelBuilder.Entity<Entity>()
            .HasOptional(e => e.ParentEntity)
            .WithMany()
            .HasForeignKey(e => e.ParentEntityId );

私のためにこのデータベースを作成しました:

ここに画像の説明を入力してください

于 2012-09-27T14:54:49.267 に答える