EntityFramework 关于不同上下文的错误。对我来说看起来像相同的上下文

EntityFramework Error about Different Contexts. Looks like the same context to me

我正在尝试 运行 以下 LINQ 查询并得到指示的错误:

var x = (from o in _dataService.Outputs join ov in _dataService.OperandValues on o.OutputID equals ov.OutputID select o).ToList();

The specified LINQ expression contains references to queries that are associated with different contexts.

我知道我不能在同一个查询中使用两个不同的上下文,但是我看不出这个 LINQ 查询是如何使用多个上下文的。当然,同一上下文中的两个不同表。但我不明白为什么会出现此错误。如果您能提供任何见解,我将不胜感激。

谢谢。

输出内容class:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Dematic.Tools.QMA.DataAccess.QMA
{
    using System;
    using System.Collections.Generic;

    public partial class Output: QmaEntityBase
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Output()
        {
            this.OperandValues = new HashSet<OperandValue>();
            this.OutputXBrands = new HashSet<OutputXBrand>();
            this.OutputCountries = new HashSet<OutputCountry>();
            this.HistoryOperandValues = new HashSet<HistoryOperandValue>();
            this.HistoryOutputs = new HashSet<HistoryOutput>();
            this.ScenarioOptions = new HashSet<ScenarioOption>();
        }

        public System.Guid OutputID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public System.Guid RuleSetID { get; set; }
        public Nullable<System.Guid> OriginalOutputID { get; set; }
        public System.Guid ServiceCatalogID { get; set; }
        public System.Guid QuotingOfficeID { get; set; }
        public System.Guid StatusID { get; set; }
        public int RecipeID { get; set; }
        public int SequenceNumber { get; set; }
        public bool IsSyncedToOwner { get; set; }
        public System.Guid CalculationMethodID { get; set; }
        public System.Guid CalculationLevelID { get; set; }
        public string ToolTip { get; set; }
        public Nullable<bool> IsCtsControlled { get; set; }
        public string Notes { get; set; }
        public string SourceApplication { get; set; }
        public Nullable<System.DateTime> CreateDateTime { get; set; }
        public string CreateUser { get; set; }
        public Nullable<System.DateTime> ModifyDateTime { get; set; }
        public string ModifyUser { get; set; }
        public Nullable<System.Guid> LaborLevelID { get; set; }
        public System.Guid WBSID { get; set; }
        public Nullable<System.Guid> BridgingClassificationTypeID { get; set; }
        public Nullable<System.Guid> TradeDisciplineClassificationTypeID { get; set; }
        public Nullable<int> ProjectTimelineTypeID { get; set; }
        public System.Guid AttributeID { get; set; }
        public string PopulatedRecipe { get; set; }
        public Nullable<int> OutputCurrencyID { get; set; }

        public virtual WBS WBS { get; set; }
        public virtual Office Office { get; set; }
        public virtual BridgingClassificationType BridgingClassificationType { get; set; }
        public virtual CalculationLevel CalculationLevel { get; set; }
        public virtual CalculationMethod CalculationMethod { get; set; }
        public virtual LaborLevel LaborLevel { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<OperandValue> OperandValues { get; set; }
        public virtual Output ParentOutput { get; set; }
        public virtual ProjectTimelineType ProjectTimelineType { get; set; }
        public virtual Recipe Recipe { get; set; }
        public virtual RuleSet RuleSet { get; set; }
        public virtual ServiceCatalog ServiceCatalog { get; set; }
        public virtual Status Status { get; set; }
        public virtual TradeDisciplineClassificationType TradeDisciplineClassificationType { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<OutputXBrand> OutputXBrands { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<OutputCountry> OutputCountries { get; set; }
        public virtual Attribute Attribute { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<HistoryOperandValue> HistoryOperandValues { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<HistoryOutput> HistoryOutputs { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<ScenarioOption> ScenarioOptions { get; set; }
        public virtual CurrencyType CurrencyType { get; set; }
    }
}

OperandValue 的内容class:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Dematic.Tools.QMA.DataAccess.QMA
{
    using System;
    using System.Collections.Generic;

    public partial class OperandValue : QmaEntityBase
    {
        public System.Guid OperandValueID { get; set; }
        public System.Guid OutputID { get; set; }
        public Nullable<System.Guid> AttributeID { get; set; }
        public int SequenceNumber { get; set; }
        public string Notes { get; set; }
        public string Tag1 { get; set; }
        public string Tag2 { get; set; }
        public Nullable<decimal> NumericValue { get; set; }
        public string TextValue { get; set; }
        public string SourceApplication { get; set; }
        public Nullable<System.DateTime> CreateDateTime { get; set; }
        public string CreateUser { get; set; }
        public Nullable<System.DateTime> ModifyDateTime { get; set; }
        public string ModifyUser { get; set; }
        public decimal InternalValue { get; set; }

        public virtual Attribute Attribute { get; set; }
        public virtual Output Output { get; set; }
    }
}

根据对原始问题的评论,在底层 IDataService 实现中确实使用了两个不同的数据库上下文,与 UnitOfWork 对象相关。要解决该错误,您需要了解如何使用共享数据库上下文。