Entity Framework 流利 API 中的两个 FK
Two FKs in Entity Framework Fluent API
我正在努力连接两个非常简单的 tables:
[Table("Item")]
public class Item
{
[Key]
[Column("itemId", TypeName = "int")]
public int ItemId { get; set; }
}
[Table("LinkedItems")]
public class LinkedItem
{
[Key]
[Column("itemId", TypeName = "int")]
public int ItemId { get; set; }
[Column("parentItemId", TypeName = "int")]
public int ParentItemId { get; set; }
}
LinkedItem
中有两个外键table:
itemId
,指向tableItem
显示自己
parentItemId
,指向tableItem
显示其父
我想完成这样的事情:
[Table("Item")]
public class Item
{
[Key]
[Column("itemId", TypeName = "int")]
public int ItemId { get; set; }
public ICollection<LinkedItem> ChildItems { get; set; }
public LinkedItem LinkedItem { get; set; }
}
[Table("LinkedItems")]
public class LinkedItem
{
[Key]
[Column("itemId", TypeName = "int")]
public int ItemId { get; set; }
[Column("parentItemId", TypeName = "int")]
public int ParentItemId { get; set; }
public Item Item { get; set; }
public Item ParentItem { get; set; }
}
我成功创建了从 Item 到 LinkedItem 的连接:
modelBuilder.Entity<Item>().HasOptional(i => i.LinkedItem).WithRequired(i => i.Item);
但我无法添加其他连接。你能帮我对这两个 table 进行 Fluent API 配置吗?
为此不需要 LinkedItem class。你可以简单地用一个 class.
来完成这个
public class Item
{
public int Id { get; set; }
public int ParentItemId { get; set; }
public virtual Item ParentItem { get; set; }
public virtual ICollection<Item> ChildItems { get; set; }
}
modelBuilder.Entity<Item>()
.HasRequired(entity => entity.ParentItem)
.WithMany(entity => entity.ChildItems)
.HasForeignKey(entity => entity.ParentItemId);
否则只需添加:
modelBuilder.Entity<Item>()
.HasMany(entity => entity.ChildItems)
.WithRequired(entity => entity.ParentItem)
.HasForeignKey(entity => entity.ParentItemId);
编辑:扩展答案。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Item>()
.HasOptional(entity => entity.LinkedItem)
.WithRequired(entity => entity.Item);
modelBuilder.Entity<Item>()
.HasMany(entity => entity.ChildItems)
.WithRequired(entity => entity.ParentItem)
.HasForeignKey(entity => entity.ParentItemId);
}
[Table("Item")]
public class Item
{
[Key]
public int ItemId { get; set; }
public virtual ICollection<LinkedItem> ChildItems { get; set; }
public virtual LinkedItem LinkedItem { get; set; }
}
[Table("LinkedItem")]
public class LinkedItem
{
[Key]
public int ItemId { get; set; }
public int ParentItemId { get; set; }
public virtual Item Item { get; set; }
public virtual Item ParentItem { get; set; }
}
这让我得到了我设置的测试表的结果:
我正在努力连接两个非常简单的 tables:
[Table("Item")]
public class Item
{
[Key]
[Column("itemId", TypeName = "int")]
public int ItemId { get; set; }
}
[Table("LinkedItems")]
public class LinkedItem
{
[Key]
[Column("itemId", TypeName = "int")]
public int ItemId { get; set; }
[Column("parentItemId", TypeName = "int")]
public int ParentItemId { get; set; }
}
LinkedItem
中有两个外键table:
itemId
,指向tableItem
显示自己parentItemId
,指向tableItem
显示其父
我想完成这样的事情:
[Table("Item")]
public class Item
{
[Key]
[Column("itemId", TypeName = "int")]
public int ItemId { get; set; }
public ICollection<LinkedItem> ChildItems { get; set; }
public LinkedItem LinkedItem { get; set; }
}
[Table("LinkedItems")]
public class LinkedItem
{
[Key]
[Column("itemId", TypeName = "int")]
public int ItemId { get; set; }
[Column("parentItemId", TypeName = "int")]
public int ParentItemId { get; set; }
public Item Item { get; set; }
public Item ParentItem { get; set; }
}
我成功创建了从 Item 到 LinkedItem 的连接:
modelBuilder.Entity<Item>().HasOptional(i => i.LinkedItem).WithRequired(i => i.Item);
但我无法添加其他连接。你能帮我对这两个 table 进行 Fluent API 配置吗?
为此不需要 LinkedItem class。你可以简单地用一个 class.
来完成这个public class Item
{
public int Id { get; set; }
public int ParentItemId { get; set; }
public virtual Item ParentItem { get; set; }
public virtual ICollection<Item> ChildItems { get; set; }
}
modelBuilder.Entity<Item>()
.HasRequired(entity => entity.ParentItem)
.WithMany(entity => entity.ChildItems)
.HasForeignKey(entity => entity.ParentItemId);
否则只需添加:
modelBuilder.Entity<Item>()
.HasMany(entity => entity.ChildItems)
.WithRequired(entity => entity.ParentItem)
.HasForeignKey(entity => entity.ParentItemId);
编辑:扩展答案。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Item>()
.HasOptional(entity => entity.LinkedItem)
.WithRequired(entity => entity.Item);
modelBuilder.Entity<Item>()
.HasMany(entity => entity.ChildItems)
.WithRequired(entity => entity.ParentItem)
.HasForeignKey(entity => entity.ParentItemId);
}
[Table("Item")]
public class Item
{
[Key]
public int ItemId { get; set; }
public virtual ICollection<LinkedItem> ChildItems { get; set; }
public virtual LinkedItem LinkedItem { get; set; }
}
[Table("LinkedItem")]
public class LinkedItem
{
[Key]
public int ItemId { get; set; }
public int ParentItemId { get; set; }
public virtual Item Item { get; set; }
public virtual Item ParentItem { get; set; }
}
这让我得到了我设置的测试表的结果: