Bug #44801 Entity Framework - Table per type inheritance problem
Submitted: 11 May 2009 18:41 Modified: 18 May 2009 19:47
Reporter: Szendrő Sándor Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:6.0.3.0 OS:Any
Assigned to: CPU Architecture:Any
Tags: entity framework, Inheritance

[11 May 2009 18:41] Szendrő Sándor
Description:
There is a problem using table per type inheritance if the base entity has a primary key with an auto-incrementing identity column. Inserting a new child entity results in the exception: {"The specified value is not an instance of a valid constant type\r\nParameter name: value"}. If i turn off auto-inc and delete StoreGeneratedPattern="Identity" from the ssdl, the insert works fine.
Using MSSQL the same mapping works fine.

How to repeat:
Here is a very simple edmx:

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
      <Schema Namespace="tesztModel1.Store" Alias="Self" Provider="MySql.Data.MySqlClient" ProviderManifestToken="5.0" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
        <EntityContainer Name="tesztModel1StoreContainer">
          <EntitySet Name="base" EntityType="tesztModel1.Store.base" store:Type="Tables" Schema="teszt" />
          <EntitySet Name="child" EntityType="tesztModel1.Store.child" store:Type="Tables" Schema="teszt" />
        </EntityContainer>
        <EntityType Name="base">
          <Key>
            <PropertyRef Name="base_id" />
          </Key>
          <Property Name="base_id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
          <Property Name="field1" Type="varchar" Nullable="false" MaxLength="45" />
        </EntityType>
        <EntityType Name="child">
          <Key>
            <PropertyRef Name="child_id" />
          </Key>
          <Property Name="child_id" Type="int" Nullable="false" />
          <Property Name="field2" Type="varchar" Nullable="false" MaxLength="45" />
        </EntityType>
      </Schema>
    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema Namespace="tesztModel1" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
        <EntityContainer Name="tesztEntities1">
          <EntitySet Name="base_table" EntityType="tesztModel1.base_table" />
        </EntityContainer>
        <EntityType Name="base_table">
          <Key>
            <PropertyRef Name="base_id" />
          </Key>
          <Property Name="base_id" Type="Int32" Nullable="false" />
          <Property Name="field1" Type="String" Nullable="false" />
        </EntityType>
        <EntityType Name="child_table" BaseType="tesztModel1.base_table">
          <Property Name="field2" Type="String" Nullable="false" />
        </EntityType>
      </Schema>
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
      <Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
        <EntityContainerMapping StorageEntityContainer="tesztModel1StoreContainer" CdmEntityContainer="tesztEntities1">
          <EntitySetMapping Name="base_table">
            <EntityTypeMapping TypeName="IsTypeOf(tesztModel1.base_table)">
              <MappingFragment StoreEntitySet="base">
                <ScalarProperty Name="base_id" ColumnName="base_id" />
                <ScalarProperty Name="field1" ColumnName="field1" />
              </MappingFragment>
            </EntityTypeMapping>
          <EntityTypeMapping TypeName="IsTypeOf(tesztModel1.child_table)">
              <MappingFragment StoreEntitySet="child">
                <ScalarProperty Name="base_id" ColumnName="child_id" />
                <ScalarProperty Name="field2" ColumnName="field2" />
              </MappingFragment>
            </EntityTypeMapping></EntitySetMapping>
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>
  </edmx:Runtime>
  </edmx:Edmx>

This code fails on the update:

 tesztEntities1 context = new tesztEntities1();
 child_table c = new child_table();
 c.field1 = "test1";
 c.field2 = "test2";
 context.AddTobase_table(c);
 context.SaveChanges();
[11 May 2009 18:50] Szendrő Sándor
Simple test project showing the error

Attachment: inheritance_bug.zip (application/x-zip-compressed, text), 4.46 KiB.

[12 May 2009 7:12] Tonci Grgin
Hi and thanks for your report.

Your test case is missing files, can you please attach complete one. Also, I do not see references to MySQL.Data & Entity libraries. Is it by chance or you wanted it that way?
[12 May 2009 19:15] Szendrő Sándor
Added the missing files and a script to create the database

Attachment: inheritance_bug_20090512.zip (application/x-zip-compressed, text), 9.58 KiB.

[12 May 2009 19:18] Szendrő Sándor
The missing references are not intentional, but they don't seem to affect the problem. I uploaded the missing files.
[13 May 2009 6:02] John Prado
If you're using unigned int as your autoincrement you'll get this error.

See the release notes for 6.0.3 MySQL Connector for .Net

If informs that the unsigned int are not supported for EF int this version.
[13 May 2009 6:02] John Prado
If you're using unsigned int as your autoincrement you'll get this error.

See the release notes for 6.0.3 MySQL Connector for .Net

It informs that the unsigned int are not supported for EF int this version.
[13 May 2009 13:21] Simon Hodgson
Is there any form of work-around for this? How long to wait for 6.0.4?
[18 May 2009 19:47] Szendrő Sándor
Sorry, i didn't notice that unsigned integers are not supported.
[18 May 2009 20:00] John Prado
The only way to get around this is remove the "unsigned" for your integers as long they're the primary keys.

Side effects: your cascades won't work.

If you set a foreign key "ON DELETE CASCADE" the table rows will not delete for the child table, the foreign key so will be set to null.
[30 Jun 2009 18:23] Boris Prpic
Still present in 6.0.4.

Is it going to be fixed soon ?
[4 Oct 2009 16:30] Eduardo Castro
Still present in 6.1.2.
[25 Jan 2010 14:16] fahad khalil
hey guys;

a sincere advice for you: Why don't you JUST PUT following text with .NET connector?

"Support for Entity Framework is VERY primitive, please use it for student-subjects type of database. Kindly do not consider it using for serious development as we ARE STILL unable to sort out VERY BASIC things like:

- we DO NOT support unsigned stuff
- we DO NOT support unsigned columns as FK
- blob cannot store more then few KB;
- cannot compare null object with a column with a LEGAL null value
- unable to write VERY PRIMITIVE check to return date as null if value in column is 0000-00-00 00:00:00
- if you use Visual Studio; sorry; we hate Microsoft, we will NOT LET you import more then two or three tables (for Micky Mouse type of tables, we can allow five; but thats it) - we will throw TIME OUT error on your face ... unless you are smart enough to change the connection time in connection string"

Damn!
[25 Jan 2010 15:40] Tonci Grgin
Since Fahad didn't even read .NET FW specs I will not dignify his post with an answer.
[19 Apr 2010 2:35] Marcos Boyington
I would be happy to look into fixing this bug with some guidance.  I'm not sure where to begin looking - it seems that the exception (as well as the inner exception) happen inside of EF, so I wouldn't know where to begin.
[19 Apr 2010 6:05] Tonci Grgin
Marcos, this is *not* a bug so I do not see what's there to fix.
[19 Apr 2010 22:47] Eduardo Castro
Not a bug? So, why does it throw an weird exception when we use that feature?
[20 Apr 2010 2:12] Marcos Boyington
Tonci: The issue I'm seeing may be unrelated, but I'm seeing the same exception.

I would assume it's because all of my primary keys are unsigned bigints.

Do you mean to say, then, that MySQL unsigned bigint primary auto-increment keys are not supported, and that's not a bug?
[20 Apr 2010 6:45] Tonci Grgin
Marcos, what you are forgetting is that we did not wrote the *framework*.

> Do you mean to say, then, that MySQL unsigned bigint primary auto-increment keys are not supported, and that's not a bug?

Exactly, framework was written for MS software and, since neither MSSQL nor Access support bigints, enums, unsigned types, last_insert_id and so on, neither does framework. We are doing the best we can to work around these limitations in the *framework* but we can not write it from the scratch. After all, think what word "framework" means...
IMO, this discussion seems to me like shouting at Ferrari car dealer for not being able to transfer building material with their cars. After all, it has engine and 4 tires, much like a truck does, right?
[20 Apr 2010 18:27] Marcos Boyington
Tonci: Fair enough.  I hadn't realized that this was purely an entity framework limitation - I guess having seen that the exception was purely inside of EF, I should've assumed as much!

Thanks for your feedback and help, it's greatly appreciated.

It blows my mind that entity framework does not support unsigned types...