2

I have a two level heirarchy of inheritance defined in my EF5 edmx file.

A simple example of the class structure:

A -> B
A -> C
A -> D -> E
A -> D -> F

Where bold classes are abstract (mustinherit).

Class A defines several properties that are used by all other classes, with each of the classes adding some data of their own.

The table has two discriminator columns, one for the first level of mapping (A -> B, C or D) and one for the second set (D -> E or F).

The edmx has no errors, the code runs fine, I can instantiate any (non-abstract) class and query the database and bring back all the data for each class correctly.

However when I attempt to create new records in the database the properties set in the lowest level classes (E and F) are not saved into the table.

here is an example of the code I run (VB, sorry):

Dim _base as A
Select case condition
    Case E
        dim _concrete as E = _context.A.Create(of E)
        _concrete.UniqueProperty = "Value"
        _base = _concrete
End Select

_base.BaseLevelProperty = "AnotherValue"

_context.Versions.Add(_base)
If _context.GetValidationErrors.Count > 0 Then Throw New Exception("context errors")
_context.SaveChanges()

Of course there are more cases for the different concrete classes.

This works fine for B and C, the unique and base properties are saved into the database. However for E or F, despite the properties being set in the object right before savechanges is called, they values do not go into the database, only NULL appears in the column where the data should be.

The strange thing is that if I manually enter data into one of these columns and query the database for, say, all objects TypeOf E then the data I manually entered is retrieved correctly.

This shows my mapping is correct, right? Why will data go one way and not another?

I know this is possible as a more complex version is discussed in this question, but it doesn't mention anything to do with my issue.

Community
  • 1
  • 1
James Ferretti
  • 171
  • 2
  • 15
  • For those finding this at a later date I solved this by moving the properties of the subclasses up to the abstarct base classes. Whilst this is obviously not ideal as it means the properties are available in all subclasses it was the only way I could get it to work. – James Ferretti Nov 08 '13 at 09:44

0 Answers0