[英]Entity Framework: Fluent API. How should I map this relationship?
我的表格如下:
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
屬性,但我無法使其工作。 我該怎么做?
提前致謝!
編輯:固定代碼差異。
這種流利將適合您:
modelBuilder.Entity<Entity>()
.HasOptional(e => e.ParentEntity)
.WithMany()
.HasForeignKey(e => e.ParentEntityId );
為我創建了這個數據庫:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.