How to query parent with properties of children and grandchildren in local data in EF Core 1.1?

c# entity-framework-core linq

Question

While importing data from legacy systems to new system I need to find items with their names and properties instead of IDs. Also the whole imported set should be saved only if everything validates. So I can't do saves in between processing different entities.

I used to have this query working in 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));

If I try to run this with Entity Framework Core 1.1 I get the following error:

System.ArgumentException occurred HResult=-2147024809
Message=Field 'Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier2[MetaUI.Models.MetaAttribute,System.Collections.Generic.IEnumerable1[Project.Models.MetaConcept]].Inner' is not defined for type '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:

What does this error mean and what is the best practice for this kind of queries with EF 1.1?

Whole model is very complex, but here are simplified versions of the relevant classes. Addition (entity classes and configuration):

 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; }


    }

Configuration:

        modelBuilder.Entity<MetaAttribute>()
                .Property(b => b.AttributeID)
               .ValueGeneratedOnAdd();
        modelBuilder.Entity<MetaSystem>()
           .HasAlternateKey(c => c.Code);
1
1
1/3/2017 11:20:26 AM

Accepted Answer

Solution for me was to exclude this exception from debugger. So I was overdebugging with Visual Studio.

4
1/3/2017 1:08:27 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow