0
我在同一個表上創建了關係,並且在OnModelCreating中聲明瞭關係。見下面的代碼。同一個表上的關係在GetAllItems時給出錯誤
試圖獲取以下代碼的所有項目var allitems = MyContext.WallItems
我收到以下錯誤:'/'應用程序中的服務器錯誤。 無法投射'WallItem'類型的對象以輸入'System.Collections.Generic.List`1 [WallItem]'。
我以錯誤的方式定義了我的關係,還是有更好的方法?
[Table("WallItems")]
public class WallItem
{
public WallItem() { Id = Guid.NewGuid(); }
/// <summary>
/// Item Id
/// </summary>
[Key]
public Guid Id { get; set; }
/// <summary>
/// Enables replies on wall items. Links to the root item.
/// </summary>
public Guid? ThreadRoot { get; set; }
/// <summary>
/// Collection of replies
/// </summary>
public virtual List<WallItem> Comments { get; set; }
}
public class MyContext : DbContext
{
public DbSet<WallItem> WallItems { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<WallItem>()
.HasOptional(c => c.Comments)
.WithMany()
.HasForeignKey(c => c.ThreadRoot);
}
}
doh ... grazie mille :) – Kulvis
有沒有辦法在同一個關係中添加一個條件?恩。 wallitem.TypeId == 2? – Kulvis