System.AccessViolationException when using TPT inheritance in Entity Framework CTP5

Posted by Ben on Stack Overflow See other posts from Stack Overflow or by Ben
Published on 2011-01-14T00:49:22Z Indexed on 2011/01/14 0:53 UTC
Read the original article Hit count: 421

Filed under:
|

Hi,

I'm using TPT inheritance in EF CTP5 with SQL CE 4.

I have an abstract base class "Widget" with common properties like "Title".

I can successfully save concrete Widget implementations e.g. "TwitterWidget".

However, I have a problem retrieving all widgets (or rather ALL widget implementations).

My repository exposes the following:

public IQueryable<Widget> GetAll();

This effectively returns the IDbSet from the DbContext.

The following queries work fine:

  repo.GetAll().ToList();
  repo.GetAll().Where(w => w.Title == "Test").ToList();
  repo.GetAll().SingleOrDefault(w => w.Title == "Test");
  repo.GetAll().Where(w => w.Title == "Test").OrderBy(x => x.Title).ToList();

However, if I write the following query:

repo.GetAll().OrderBy(w => w.Title);

I get the following error:

Test 'PlanetCloud.Portfolio.Tests.Data.PersistenceTestFixture.Can_get_widgets' failed: System.AccessViolationException : Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Data.SqlServerCe.NativeMethodsHelper.GetValues(IntPtr pSeCursor, Int32 seGetColumn, IntPtr prgBinding, Int32 cDbBinding, IntPtr pData, IntPtr pError)
at System.Data.SqlServerCe.NativeMethods.GetValues(IntPtr pSeCursor, Int32 seGetColumn, IntPtr prgBinding, Int32 cDbBinding, IntPtr pData, IntPtr pError)
at System.Data.SqlServerCe.SqlCeDataReader.FetchValue(Int32 index)
at System.Data.SqlServerCe.SqlCeDataReader.IsDBNull(Int32 ordinal)
at lambda_method(Closure , Shaper )
at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
at lambda_method(Closure , Shaper )
at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

However, I can execute the following query without problems:

var widgets = repo.GetAll().OfType<Widget>().OrderBy(w => w.Title).ToList();

So if I specify the type as the base class prior to my orderby clause it works.

The question is why?

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about CTP5