0

I have a very strange problem when using Entity Framework with Code First approach. I have disabled lazy loading and are using include statements when I need. Unfortunately I cannot show you the code, but will try to explain as detailed I can.

I have two tables where one is referring to the other:

TableA (ID, Name)
TableB (ID, ForeignKeyTableA)

The corresponding classes:

public class A
{
   public int ID { get; set; }
   public string Name { get; set; }
}

public class B
{
   public int ID { get; set; }
   public A ClassA { get; set; }
}

I can load A and B by themselves without and problem, but when I write for example: Include(p => p.A) when loading B and finally calling ToList() it will cause an OutOfMemoryException after a while.

The tables aren't that big. Table A have around 600 records and table B 5 records.

The mapping for class A looks like this:

this.ToTable("TableA");
this.HasKey(p => p.ID);
this.Property(p => p.ID).HasColumnName("ID");
this.Property(p => p.ID).HasColumnName("Name");

The mapping for class B looks like this:

this.ToTable("TableB");
this.HasKey(p => p.ID);
this.Property(p => p.ID).HasColumnName("ID");

HasRequired(p => p.A).WithOptional()
   .Map(m => m.MapKey("ForeignKeyTableA"));

I need to map them because the column and table names aren't by convention. Include can be used on other objects without any problems.

The weirdest thing is that when I am using Include() the database will never be hit.

Anyone got suggestions? Because I am really running out of ideas now.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • If you can't show us the original code, can you reproduce this problem with a simplified example of the original code? You may find the issue by removing one thing at a time from original code trying to get down to something that reproduces the issue and is sharable. – Denise Skidmore Jun 25 '14 at 13:28
  • Does it also happen when you do `query.ToString()`? That would indicate that query generation is somehow messed up. – Gert Arnold Jun 25 '14 at 13:31
  • I removed all the complex properties and only had ID and Name on the classes, but it was still performing the same way. This is happening on all classes that are referring to A. – Fredrik Lindfors Jun 25 '14 at 13:35
  • @GertArnold Yes, it is happening with ToString() as well. Without include I get a SQL query, with Include it causes OutOfMemoryException. – Fredrik Lindfors Jun 25 '14 at 13:38
  • And the real code has only one include? This can happen with large numbers of includes. – Gert Arnold Jun 25 '14 at 13:47
  • @GertArnold Yes, only one include. If I switch the include to another object it works. This only happens with object B. – Fredrik Lindfors Jun 26 '14 at 07:03
  • It's clearly something in this "A" class that causes this, but that's hard to tell without seeing the real thing. Can you disclose some more parts that may play a role in this? – Gert Arnold Jun 26 '14 at 07:08
  • I think I found something. The thing is that object A is referenced by a lot of objects in the model (not necessarily the same ID). I removed ALL the references except one, and it worked. Now I'm adding them one by one and it gets slower and slower to make queries. Why is this happening even if I don't include the properties? @GertArnold – Fredrik Lindfors Jun 26 '14 at 15:10

0 Answers0