Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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
C# 在EF7中执行即时加载时发生ArgumentException_C#_Entity Framework_Entity Framework Core - Fatal编程技术网

C# 在EF7中执行即时加载时发生ArgumentException

C# 在EF7中执行即时加载时发生ArgumentException,c#,entity-framework,entity-framework-core,C#,Entity Framework,Entity Framework Core,我正在尝试加载具有多个导航属性的模型。大多数被引用的模型都可以正常加载,但是当试图包含()被引用的列表时,项目会崩溃,出现System.ArgumentException-静态字段需要空实例,非静态字段需要非空实例 装载 var project = db .Projects .Include(x => x.Fittings) // offending Include .Include(x => x.CostParameter) // works

我正在尝试加载具有多个导航属性的模型。大多数被引用的模型都可以正常加载,但是当试图
包含()被引用的列表时,项目会崩溃,出现
System.ArgumentException-静态字段需要空实例,非静态字段需要非空实例

装载

var project =
    db
    .Projects
    .Include(x => x.Fittings) // offending Include
    .Include(x => x.CostParameter) // works
    .Include(x => x.ProcessParameter) // works
    .Include(x => x.CreatedByUser) // works
    .Single(x => x.Id == package.IsPartOfProjectId);
为了使事情更复杂一点,所讨论的列表是一个抽象类。此外,该列表还可以包含另一个抽象类的子类

modelBuilder.Entity<ButtWrap>().HasBaseType<Fitting>();
modelBuilder.Entity<Elbow>().HasBaseType<ButtWrap>();
modelBuilder.Entity().HasBaseType();
modelBuilder.Entity().HasBaseType();
模型配置

modelBuilder.Entity<Project>()
    .HasMany(x => x.Fittings)
    .WithOne(x => x.Project)
    .IsRequired()
    .OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity()
.HasMany(x=>x.Fittings)
.WithOne(x=>x.Project)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
项目模型

public sealed class Project : CommonTypes.ProjectBase
{
    public int ProjectTrackingNumber { get; set; }
    public string Name { get; set; }
    public bool IsManufacture { get; set; }
    public bool IsCostEstimate { get; set; }  
    public string QueueStatus { get; set; }    
    public DateTime CreatedAt { get; set; }

    public List<Fitting> Fittings { get; set; } = new List<Fitting>();
    public Meta Meta { get; set; }
    public FittingsInterfaceUser CreatedByUser { get; set; }

    public CostParameter CostParameter { get; set; }
    public ProcessParameter ProcessParameter { get; set; }
}

public class ProjectBase : EntityBaseIdentifier, IAggregateRoot
{
    public ProjectBase(long entityId) : base(entityId)
    {

    }

    protected override void Validate()
    {
        throw new NotImplementedException();
    }
}
公共密封类项目:CommonTypes.ProjectBase
{
public int ProjectTrackingNumber{get;set;}
公共字符串名称{get;set;}
公共bool IsManufacture{get;set;}
公共bool IsCostEstimate{get;set;}
公共字符串队列状态{get;set;}
public DateTime CreatedAt{get;set;}
公共列表配件{get;set;}=new List();
公共元{get;set;}
公共fittingInterfaceuser CreatedByUser{get;set;}
公共成本参数成本参数{get;set;}
公共进程参数进程参数{get;set;}
}
公共类ProjectBase:EntityBaseIdentifier,IAggregateRoot
{
公共项目库(长entityId):库(entityId)
{
}
受保护的覆盖无效验证()
{
抛出新的NotImplementedException();
}
}
撇开有点复杂的结构不谈,我其实早就有了这个工作。然而,在这条路上的某个地方,我引入了一个错误


由于错误有点神秘,我真的不知道从哪里开始查找,所以欢迎所有建议。

问题当然出在我最初问题中没有发布的某个类中。我相当愚蠢地在抽象的“
Fittings
”类中添加了一个抽象字段,其中只有一个
getter
。所以,有趣的是,“神秘的错误信息”现在完全有意义了

public abstract string FittingsType { get; }
引入此字段是为了帮助遗留系统,并且始终从实现基类返回硬编码值


非常感谢所有的评论,这些评论引导我走向正确的方向

请显示
项目
类。@CodeCaster在中编辑。@VolodymyrBilyachat现在它只是一个POCO,具有数据库中存在的项目Id。请尝试将
配件
属性设置为
虚拟
@KGChristensen Try
虚拟ICollection
。不要强迫EF使用
列表