Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 当GetAllItems_Entity Framework_Entity Relationship - Fatal编程技术网

Entity framework 当GetAllItems

Entity framework 当GetAllItems,entity-framework,entity-relationship,Entity Framework,Entity Relationship,我在同一个表上创建了一个关系,该关系在OnModelCreating中声明。请参阅下面的代码 尝试获取具有以下代码的所有项目时var allitems=MyContext.WallItems 我得到以下错误:“/”应用程序中的服务器错误。 无法将“WallItem”类型的对象强制转换为“System.Collections.Generic.List`1[WallItem]”类型 我是用错误的方式定义了我的关系,还是有更好的方式 [Table("WallItems")] public class

我在同一个表上创建了一个关系,该关系在OnModelCreating中声明。请参阅下面的代码

尝试获取具有以下代码的所有项目时
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);
    }
}
[表格(“墙项”)]
公共类挂件
{
公共墙项(){Id=Guid.NewGuid();}
/// 
///项目Id
/// 
[关键]
公共Guid Id{get;set;}
/// 
///启用对墙项目的答复。指向根项目的链接。
/// 
公共Guid?ThreadRoot{get;set;}
/// 
///收集答复
/// 
公共虚拟列表注释{get;set;}
}
公共类MyContext:DbContext
{
公共数据库集WallItems{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.has可选(c=>c.Comments)
.有很多
.HasForeignKey(c=>c.ThreadRoot);
}
}

您的Fluent API配置有错误。这将有助于:

modelBuilder.Entity<WallItem>()
    .HasMany(c => c.Comments)
    .WithOptional()
    .HasForeignKey(c => c.ThreadRoot);
modelBuilder.Entity()
.HasMany(c=>c.Comments)
.WithOptional()
.HasForeignKey(c=>c.ThreadRoot);

是否有办法在同一关系中添加条件?例如,其中wallitem.TypeId==2?