none
IOException: Unable to read data — Entity Framework Core RRS feed

  • Question

  • I have an ASP.NET Core 2.0 project using Entity Framework Core 2.0 with an Azure SQL Database.  The EF files were generated in database-first mode. I can successfully retrieve a top-level record with:

    FooList = Context.FooTable.Where(x => x.FooID == Fooid)
        .ToList();

    There is a related table called FooAttributeTable having the same primary key which should also be retrieved. So I add an Include clause as I have seen in tutorials:

    FooList = Context.FooTable.Where(x => x.FooID == Fooid)
        .Include(x => x.FooAttributeTable)
        .ToList();

    This retrieval works as expected, returning an empty sub-list for the related table, if the related table has no entry for this key.  But if it has an entry, the whole retrieval fails with the message "IOException: Unable to read data from the transport connection: The connection was closed."

    Here are the generated entity classes:

    public partial class FooTable
    {
        public FooTable()
        {
            FooAttributeTable = new HashSet<FooAttributeTable>();
        } 
    
        public int FooID { get; set; } 
    
        public virtual ICollection<FooAttributeTable> FooAttributeTable { get; set; }
    }
    
    public partial class FooAttributeTable
    {
        public int FooID { get; set; }
        public int Attribute { get; set; } 
    
        public virtual AttributeEnum AttributeNavigation { get; set; }
        public virtual FooTable Foo { get; set; }
    }

    FooAttributeTable is complete. FooTable shows just the parts that deal with FooAttributeTable; I cannot post the entire FooTable because it is a large amount of proprietary (albeit generated) code.

    Debugging into this has shown a circular dependency in the returned list. The returned FooTable contains an entry for a FooAttributeTable, which contains an entry for the FooTable, and so on, to an unknown number of levels. So the question becomes how to avoid this circularity.  In the database there is just one foreign key relationship between the two tables.

    I removed the foreign key relationship from the database and regenerated the EF files.  As expected, the links between the tables, specifically the FooAttributeTable property of FooTable and the Foo property of FooAttributeTable, were removed in the generated files and the .Include clause would no longer compile.  What am I missing?

    Monday, February 25, 2019 3:11 PM

Answers

  • It's database-first as I mentioned earlier, but that tutorial page gave me the necessary clues (feels like I've read innumerable tutorials but this one is much clearer than some).  The generated files had double links, from the dependent table to the primary table and vice versa.  By removing the generated links one at a time I was able to find a combination that works.  I conclude that there is a bug in the EF Core 2.0 file generation that does this.  Thanks so much for the help!
    • Marked as answer by John Boncek Tuesday, February 26, 2019 3:49 PM
    Tuesday, February 26, 2019 3:47 PM

All replies

  • FooList = Context.FooTable.Where(x => x.FooID == Fooid)
        .Include(x => x.FooAttributeTable)
        .ToList();

    I don't see how you can be coming back with a Foolist. It seems to me that you should be coming back with one Foo.

    var foo = Context.FooTable.Where(x => x.FooID == Fooid)

        .Include(x => x.FooAttributeTable)
        .singleordeefault();

    If (foo !- null)

    {
       // work wit Foo properties.
        if (Foo.FooAttributeTables.Count > 0)
        {
             //loop on Foo.FooAttributeTables
        }
    }



    • Edited by DA924x Monday, February 25, 2019 9:49 PM
    Monday, February 25, 2019 9:48 PM
  • Thanks for replying.  It was a list of one, which worked in some other cases (I'm new to EF).  I tried it with SingleOrDefault as you suggested and got the same exception.  Debugging into the returned object again shows a seemingly endless recursion of nested objects as I initially described.

    Does the generated foreign key stuff look right?  This is a one-to-many relationship, with each FooTable entry having potentially many corresponding FooAttributeTable entries.  The primary key of FooTable is FooID.  The primary key of FooAttributeTable is FooID + Attribute.


    • Edited by John Boncek Wednesday, March 13, 2019 4:46 PM
    Monday, February 25, 2019 10:26 PM
  • I don't use Code first, but your models don't look right particularly concerning FooID is both classes.

    You should review the link.

    http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

     
    Tuesday, February 26, 2019 12:07 AM
  • It's database-first as I mentioned earlier, but that tutorial page gave me the necessary clues (feels like I've read innumerable tutorials but this one is much clearer than some).  The generated files had double links, from the dependent table to the primary table and vice versa.  By removing the generated links one at a time I was able to find a combination that works.  I conclude that there is a bug in the EF Core 2.0 file generation that does this.  Thanks so much for the help!
    • Marked as answer by John Boncek Tuesday, February 26, 2019 3:49 PM
    Tuesday, February 26, 2019 3:47 PM