如何在EF Core 1.1的本地數據中查詢具有子孫子屬性的父級?

c# entity-framework-core linq

在將數據從舊系統導入新系統時,我需要查找具有其名稱和屬性而非ID的項目。此外,只有在所有內容都有效的情況下,才應保存整個導入集。所以我不能在處理不同實體之間進行保存。

我以前在EF6中使用此查詢:

      MetaAttribute fromAttribute =
                    db.Attributes
                    .Single(a =>
                        (a.Concept.Name.Equals(entityName)) &&
                        (a.Concept.System.Code.Equals(defaultSystem.Code)) &&
                        (a.Name.Equals(attributeName)))
                ??   db.Attributes.Local.Single(a => (a.Concept.Name == entityName) && (a.Concept.System.Code == defaultSystem.Code) && (a.Name == attributeName));

如果我嘗試使用Entity Framework Core 1.1運行它,我會收到以下錯誤:

發生了System.ArgumentException HResult = -2147024809
Message = 2[MetaUI.Models.MetaAttribute,System.Collections.Generic.IEnumerable + TransparentIdentifier 2[MetaUI.Models.MetaAttribute,System.Collections.Generic.IEnumerable 1 [Project.Models.MetaConcept]]。Inner'未定義類型'Microsoft.EntityFrameworkCore。 Query.EntityQueryModelVisitor + TransparentIdentifier`2 [Project.Models.MetaAttribute,Project.Models.MetaConcept]'Source = System.Linq.Expressions StackTrace:at System.Linq.Expressions.Expression.Field(Expression expression,FieldInfo field)InnerException:

這個錯誤意味著什麼,使用EF 1.1進行此類查詢的最佳做法是什麼?

整個模型非常複雜,但這裡是相關類的簡化版本。添加(實體類和配置):

 public class MetaAttribute : MetaBase, IValidatableObject
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int AttributeID { get; set; }


        [MaxLength(350)]
        public string Name { get; set; }
        [ForeignKey("ConceptID")]
        [JsonIgnore]

        public MetaConcept Concept { get; set; }
        public int? ConceptID { get; set; }
}

 public class MetaConcept : MetaBase
    {        
        [Key]
        public int ConceptID { get; set; }
        [Required]
        public int ConceptVersionID { get; set; }
        public string Title { get; set; }
        [Required]
        [MaxLength(255)]
        public string Name { get; set; }

        [JsonIgnore]
        public MetaConcept FromConcept { get; set; }

        public int? SystemID { get; set; }

        [ForeignKey("SystemID")]
        public MetaSystem System { get; set; }
        public ICollection<MetaAttribute> Attributes { get; set; }

        public MetaConcept()
        {
            Attributes = new Collection<MetaAttribute>();

        }
    }   

  public class MetaSystem : MetaBase
    {
        [Key]
        public int SystemID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        [Required]
        [MaxLength(400)]
        public string Code { get; set; }
    }   

 public abstract class MetaBase
    {
        [Column(TypeName = "datetime2")]

        public DateTime Created { get; set; }
        [Column(TypeName = "datetime2")]
        public DateTime? LastModified { get; set; }
        [Timestamp]
        public byte[] RowVersion { get; set; }

        [MaxLength(300)]
        public string LastChangedBy { get; set; }


    }

組態:

        modelBuilder.Entity<MetaAttribute>()
                .Property(b => b.AttributeID)
               .ValueGeneratedOnAdd();
        modelBuilder.Entity<MetaSystem>()
           .HasAlternateKey(c => c.Code);

一般承認的答案

我的解決方案是從調試器中排除此異常。所以我對Visual Studio進行了過度調試。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow