Bug #44801 Entity Framework - Table per type inheritance problem
Submitted: 11 May 20:41 Modified: 18 May 21:47
Reporter: Szendrő Sándor
Status: Closed
Category:Connector/Net Severity:S3 (Non-critical)
Version:6.0.3.0 OS:Any
Assigned to: Tonci Grgin Target Version:
Tags: entity framework, Inheritance

[11 May 20: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 20:50] Szendrő Sándor
Simple test project showing the error

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

[12 May 9: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 21: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 21: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 8: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 8: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 15:21] Simon Hodgson
Is there any form of work-around for this? How long to wait for 6.0.4?
[18 May 21:47] Szendrő Sándor
Sorry, i didn't notice that unsigned integers are not supported.
[18 May 22: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 20:23] Boris Prpic
Still present in 6.0.4.

Is it going to be fixed soon ?
[4 Oct 18:30] Eduardo Castro
Still present in 6.1.2.