Multiple Inheritance with Entity Framework with TPH - entity-framework

Further to this question:
Entity Framework TPH with multiple abstract inheritance and VS.2008 sp1 .net 3.5 c#
I decided to add Organizations and a School. Organization(abstract) inherits from Party, and School(concrete) inherits from Organization.
I get the error:
Error 1 Error 3034: Problem in Mapping Fragments starting at lines 73, 93: Two entities with different keys are mapped to the same row. Ensure these two mapping fragments do not map two groups of entities with different keys to the same group of rows.
C:\Users\xxx\Documents\Visual Studio 2008\Projects\TEST\TEST\Model1.edmx 74 29 TEST
I've seen the 3034 errors alot along my travels in EF. But typically they relate to navigational properties. I haven't seen errors like this in inheritence.
Here's my edmx xml:
<?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="test_1Model.Store" Alias="Self"
Provider="System.Data.SqlClient" ProviderManifestToken="2005"
xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"
xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
<EntityContainer Name="test_1ModelStoreContainer">
<EntitySet Name="Student" EntityType="test_1Model.Store.Student"
store:Type="Tables" Schema="dbo" />
</EntityContainer>
<EntityType Name="Student">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="int" Nullable="false"
StoreGeneratedPattern="Identity" />
<Property Name="PartyInfo" Type="varchar(max)" Nullable="false" />
<Property Name="PersonInfo" Type="varchar(max)" Nullable="true" />
<Property Name="StudInfo" Type="varchar(max)" Nullable="true" />
<Property Name="OrgInfo" Type="varchar(max)" Nullable="true" />
<Property Name="SchoolInfo" Type="varchar(max)" Nullable="true" />
<Property Name="TypeOfParty" Type="varchar(max)" Nullable="false" />
</EntityType>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="test_1Model" Alias="Self"
xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
<EntityContainer Name="test_Entities">
<EntitySet Name="PartySet" EntityType="test_1Model.Party" />
</EntityContainer>
<EntityType Name="Party" Abstract="true">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Int32" Nullable="false" />
<Property Name="PartyInfo" Type="String" Nullable="false"
MaxLength="Max" Unicode="false" FixedLength="false" />
</EntityType>
<EntityType Name="Person" BaseType="test_1Model.Party" Abstract="true" >
<Property Name="PersonInfo" Type="String" Nullable="false" />
</EntityType>
<EntityType Name="Student" BaseType="test_1Model.Person" >
<Property Name="StudInfo" Type="String" Nullable="false" />
</EntityType>
<EntityType Name="Organization" BaseType="test_1Model.Party" Abstract="true" >
<Property Name="OrgInfo" Type="String" Nullable="false" />
</EntityType>
<EntityType Name="School" BaseType="test_1Model.Organization" >
<Property Name="SchoolInfo" 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="test_1ModelStoreContainer"
CdmEntityContainer="test_Entities">
<EntitySetMapping Name="PartySet">
<EntityTypeMapping TypeName="IsTypeOf(test_1Model.Party)">
<MappingFragment StoreEntitySet="Student">
<ScalarProperty Name="PartyInfo" ColumnName="PartyInfo" />
<ScalarProperty Name="Id" ColumnName="Id" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="IsTypeOf(test_1Model.Person)">
<MappingFragment StoreEntitySet="Student">
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="PersonInfo" ColumnName="PersonInfo" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="test_1Model.Student">
<MappingFragment StoreEntitySet="Student">
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="StudInfo" ColumnName="StudInfo" />
<ScalarProperty Name="PersonInfo" ColumnName="PersonInfo" />
<ScalarProperty Name="PartyInfo" ColumnName="PartyInfo" />
<Condition ColumnName="TypeOfParty" Value="STUDENT" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="IsTypeOf(test_1Model.Organization)">
<MappingFragment StoreEntitySet="Student">
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="OrgInfo" ColumnName="OrgInfo" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="test_1Model.School">
<MappingFragment StoreEntitySet="Student">
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="OrgInfo" ColumnName="OrgInfo" />
<ScalarProperty Name="PartyInfo" ColumnName="PartyInfo" />
<ScalarProperty Name="SchoolInfo" ColumnName="SchoolInfo" />
<Condition ColumnName="TypeOfParty" Value="SCHOOL" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing"
Value="EmbedInOutputAssembly" />
</DesignerInfoPropertySet>
</edmx:Connection>
<edmx:Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true" />
</DesignerInfoPropertySet>
</edmx:Options>
<!-- Diagram content (shape and connector positions) -->
<edmx:Diagrams>
<Diagram Name="SqlServer_Model" >
<EntityTypeShape EntityType="test_1Model.Party" Width="1.5" PointX="1.25" PointY="3.25" Height="1.4033821614583326" />
<InheritanceConnector EntityType="test_1Model.Person" ManuallyRouted="false">
<ConnectorPoint PointX="2" PointY="4.6533821614583326" />
<ConnectorPoint PointX="2" PointY="5.75" />
</InheritanceConnector>
<EntityTypeShape EntityType="test_1Model.Organization" Width="1.5" PointX="3.875" PointY="5.625" Height="1.2110807291666665" />
<InheritanceConnector EntityType="test_1Model.Organization">
<ConnectorPoint PointX="2.75" PointY="3.9516910807291663" />
<ConnectorPoint PointX="4.625" PointY="3.9516910807291663" />
<ConnectorPoint PointX="4.625" PointY="5.625" />
</InheritanceConnector>
<EntityTypeShape EntityType="test_1Model.School" Width="1.5" PointX="3.875" PointY="7.875" Height="1.2110807291666657" />
<InheritanceConnector EntityType="test_1Model.School">
<ConnectorPoint PointX="4.625" PointY="6.8360807291666665" />
<ConnectorPoint PointX="4.625" PointY="7.875" />
</InheritanceConnector>
</Diagram>
</edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>

I know this question is ages old, but I spent a long time researching a similar problem in a recent adventure with EF 5.0 (and 6.0.0-beta1), and this article led me to a valid solution. Thanks to the author.
I have a complex hierarchy mapped using TPH:
A1 - B1 - C1
A1 - B1 - C2
A1 - B2 - C3
A1 - B2 - C4
A1 - B2 - C5
A1 - B2 - C6 - D1 - E1
A1 - B2 - C6 - D1 - E2
A1 - B2 - C6 - D1 - E3
A1 - B2 - C6 - D2 - E4
A1 - B2 - C6 - D2 - E5
A1 - B2 - C6 - D2 - E6
A1 - B2 - C6 - D3 - E7
A1 - B2 - C6 - D3 - E8
A1 - B2 - C6 - D3 - E9
Where bold indicates abstract, and italic indicates additional mappings specific to that class (and subclasses) to the same table (of course).
Basically the solution was to create 4 discriminator columns in the table, and map each level in the hierarchy to a different discriminator column. Note that the abstract classes in level B and below also need to be mapped and have a discriminator value specified.
A lot of time wasted on this, all the examples and documentation of TPH seem to only cover the basics. In the real world things can get a little more complex!
Hope this helps someone.

You seem to be trying to do table-per-hierarchy mapping (because I see only one table in your SSDL), but I don't see that you have set up a discriminator mapping in the CSDL. There should be a Condition node, like this:
<Condition ColumnName="NotificationType" Value="Announcement"/>
In short, if you are trying to use a table per hierarchy mapping, you should review the instructions for configuring this and follow them, because you have not completed all of the steps.
If, on the other hand, you intend to do table per type mapping, then you seem to have missed a couple of tables when you updated from your database.

Related

Issue in EF, mapping fragment,no default value and is not nullable

I am developing MVC 3 Applicaiton.
I have Model first approch.
I have Company Entity(Abstract).
Lead and Customer is inherited from the company entity.
When I tried to validate the model, Its gives an errror.
Error 41 Error 3023: Problem in mapping fragments starting at line
70:Column Companies.Status in table Companies must be mapped: It has
no default value and is not nullable.
Here is the mapping of tables.
And Here is the EDMX code in HTML View.
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="Model1.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator">
<EntityContainer Name="Model1StoreContainer">
<EntitySet Name="Companies" EntityType="Model1.Store.Companies" store:Type="Tables" Schema="dbo" />
</EntityContainer>
<EntityType Name="Companies">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="Name" Type="nvarchar(max)" Nullable="false" />
<Property Name="Status" Type="nvarchar(max)" Nullable="false" />
<Property Name="__Disc__" Type="nvarchar" MaxLength="Max" Nullable="false" />
</EntityType>
</Schema></edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="Model1" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
<EntityContainer Name="Model1Container" annotation:LazyLoadingEnabled="true">
<EntitySet Name="Companies" EntityType="Model1.Company" />
</EntityContainer>
<EntityType Name="Company" Abstract="true">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Type="String" Name="Name" Nullable="false" />
</EntityType>
<EntityType Name="Lead" BaseType="Model1.Company" >
<Property Type="String" Name="Status" Nullable="false" />
</EntityType>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs">
<EntityContainerMapping StorageEntityContainer="Model1StoreContainer" CdmEntityContainer="Model1Container">
<EntitySetMapping Name="Companies">
<EntityTypeMapping TypeName="IsTypeOf(Model1.Company)">
<MappingFragment StoreEntitySet="Companies">
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="Name" ColumnName="Name" />
<Condition ColumnName="__Disc__" Value="Company" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="Model1.Lead">
<MappingFragment StoreEntitySet="Companies">
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="Name" ColumnName="Name" />
<ScalarProperty Name="Status" ColumnName="Status" />
<Condition ColumnName="__Disc__" Value="Lead" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping></edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<edmx:Designer xmlns="http://schemas.microsoft.com/ado/2008/10/edmx">
<edmx:Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
</DesignerInfoPropertySet>
</edmx:Connection>
<edmx:Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true" />
<DesignerProperty Name="EnablePluralization" Value="True" />
<DesignerProperty Name="DatabaseGenerationWorkflow" Value="$(VSEFTools)\DBGen\Generate T-SQL Via T4 (TPH).xaml" />
</DesignerInfoPropertySet>
</edmx:Options>
<!-- Diagram content (shape and connector positions) -->
<edmx:Diagrams>
<Diagram Name="Model1" >
<EntityTypeShape EntityType="Model1.Company" Width="1.5" PointX="2.375" PointY="0.875" Height="1.2636116536458335" />
<EntityTypeShape EntityType="Model1.Lead" Width="1.5" PointX="3.375" PointY="2.625" Height="1.0992643229166665" />
<InheritanceConnector EntityType="Model1.Lead" >
<ConnectorPoint PointX="3.125" PointY="2.1386116536458335" />
<ConnectorPoint PointX="3.125" PointY="2.325" />
<ConnectorPoint PointX="4.125" PointY="2.325" />
<ConnectorPoint PointX="4.125" PointY="2.625" />
</InheritanceConnector>
</Diagram>
</edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>
Whats is the issue ?
Put the DefaultValue attribute on the SSDL Status property. It would look like this (I used the empty string):
<Property Name="Status" Type="nvarchar(max)" Nullable="false" DefaultValue=""/>
Since the base entity is abstract you won't be able to create entities of this type so this DefaultValue will not really be used but it should make EF stop complaining.
In your storage model, the Companies.Status column doesn't allow null values.
Since all entities that inherit from Company except the Lead entity will not have the Status property set, you need to either allow null in the Companies.Status column or set a default value to use for the other entities.

Entity framework stored procedure mapping results not unique entities

I have a problem where I've mapped a stored procedure named sp_getMyEntity, which takes in one parameter called Id and then maps the results to a custom entity called MyEntity
I'm using mock data to illustrate the point. When I run the stored procedure with an id of 1, I get two distinct results back from the database:
exec [dbo].[sp_getMyEntity] #Id=1
Results:
ID - AddressId
1 - 6
1 - 3
When querying the ObjectContext using LINQ, I get 2 MyEntity's back but the AddressId for both of them is 6, not 6 and 3 respectively. Here is my call using LINQ:
Entities context = new Entities();
var entities = from s in context.GetMyEntity(1)
select s;
return entities.ToList();
Here is the EDMX xml:
<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="MyProjectModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
<EntityContainer Name="MyProjectModelStoreContainer">
<EntitySet Name="MyEntitySet" EntityType="MyProjectModel.Store.MyEntity" />
</EntityContainer>
<Function Name="sp_getMyEntity" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="Id" Type="int" Mode="In" />
</Function>
<EntityType Name="MyEntity">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="int" Nullable="false" />
<Property Name="AddressId" Type="int" Nullable="true" />
</EntityType>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="MyProjectModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
<EntityContainer Name="MyProjectViewEntities" >
<EntitySet Name="MyEntitySet" EntityType="MyProjectModel.MyEntity" />
<FunctionImport Name="GetMyEntity" EntitySet="MyEntitySet" ReturnType="Collection(MyProjectModel.MyEntity)">
<Parameter Name="Id" Mode="In" Type="Int32" />
</FunctionImport>
</EntityContainer>
<EntityType Name="MyEntity">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Int32" Nullable="false" />
<Property Name="AddressId" Type="Int32" Nullable="true" />
</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="MyProjectModelStoreContainer" CdmEntityContainer="MyProjectViewEntities" >
<FunctionImportMapping FunctionImportName="GetMyEntity" FunctionName="MyProjectModel.Store.sp_getMyEntity" />
<EntitySetMapping Name="MyEntitySet">
<EntityTypeMapping TypeName="IsTypeOf(MyProjectModel.MyEntity)">
<MappingFragment StoreEntitySet="MyEntitySet">
<ScalarProperty Name="AddressId" ColumnName="AddressId" />
<ScalarProperty Name="Id" ColumnName="Id" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
</DesignerInfoPropertySet>
</edmx:Connection>
<edmx:Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true" />
</DesignerInfoPropertySet>
</edmx:Options>
<!-- Diagram content (shape and connector positions) -->
<edmx:Diagrams>
<Diagram Name="MyProjectModel" ZoomLevel="100" >
<EntityTypeShape EntityType="MyProjectModel.MyEntity" Width="1.5" PointX="5.25" PointY="0.5" Height="7.8375048828125" />
<EntityTypeShape EntityType="MyProjectModel.MyEntity" Width="1.5" PointX="5.5" PointY="1.375" Height="1.2636116536458335" /></Diagram></edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>
Any ideas why the MyEntity's are not unique?
Because the SP column which your SSDL identifies as the key is not a unique column. You cannot have a "key" with duplicate values.
As Craig stated above my Key property was not unique, so I ended up changing the SQL to look at follows:
SELECT
ROW_NUMBER() OVER(ORDER BY Id) AS KeyId
, Id
, AddressId
FROM
MyTable
WHERE
Id = #Id
Then the EDMX xml looks as follows, notice the new key is a bigint and Int64:
<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="MyProjectModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
<EntityContainer Name="MyProjectModelStoreContainer">
<EntitySet Name="MyEntitySet" EntityType="MyProjectModel.Store.MyEntity" />
</EntityContainer>
<Function Name="sp_getMyEntity" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="Id" Type="int" Mode="In" />
</Function>
<EntityType Name="MyEntity">
<Key>
<PropertyRef Name="KeyId" />
</Key>
<Property Name="KeyId" Type="bigint" Nullable="false" />
<Property Name="Id" Type="int" Nullable="false" />
<Property Name="AddressId" Type="int" Nullable="true" />
</EntityType>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="MyProjectModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
<EntityContainer Name="MyProjectViewEntities" >
<EntitySet Name="MyEntitySet" EntityType="MyProjectModel.MyEntity" />
<FunctionImport Name="GetMyEntity" EntitySet="MyEntitySet" ReturnType="Collection(MyProjectModel.MyEntity)">
<Parameter Name="Id" Mode="In" Type="Int32" />
</FunctionImport>
</EntityContainer>
<EntityType Name="MyEntity">
<Key>
<PropertyRef Name="KeyId" />
</Key>
<Property Name="KeyId" Type="Int64" Nullable="false" />
<Property Name="Id" Type="Int32" Nullable="false" />
<Property Name="AddressId" Type="Int32" Nullable="true" />
</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="MyProjectModelStoreContainer" CdmEntityContainer="MyProjectViewEntities" >
<FunctionImportMapping FunctionImportName="GetMyEntity" FunctionName="MyProjectModel.Store.sp_getMyEntity" />
<EntitySetMapping Name="MyEntitySet">
<EntityTypeMapping TypeName="IsTypeOf(MyProjectModel.MyEntity)">
<MappingFragment StoreEntitySet="MyEntitySet">
<ScalarProperty Name="AddressId" ColumnName="AddressId" />
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="KeyId" ColumnName="KeyId" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
</DesignerInfoPropertySet>
</edmx:Connection>
<edmx:Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true" />
</DesignerInfoPropertySet>
</edmx:Options>
<!-- Diagram content (shape and connector positions) -->
<edmx:Diagrams>
<Diagram Name="MyProjectModel" ZoomLevel="100" >
<EntityTypeShape EntityType="MyProjectModel.MyEntity" Width="1.5" PointX="5.25" PointY="0.5" Height="7.8375048828125" />
<EntityTypeShape EntityType="MyProjectModel.MyEntity" Width="1.5" PointX="5.5" PointY="1.375" Height="1.2636116536458335" /></Diagram></edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>

Entity Framework - inserting new entity via objectcontext does not use existing entity properties

I have an insert method on my repository like so:
public T Insert(T entity)
{
_ctx.AddObject(EntityName, entity);
_ctx.SaveChanges();
return entity;
}
If I execute the below code, the values assigned to my entity do not propagate to the SQL that is executed.
Category c = new Category();
c.Name = CLEARANCE;
c = categoryManager.Insert(c);
The SQL should be something like
INSERT INTO Category(Name) VALUES('Clearance')
Instead, the following SQL is being executed
insert [dbo].[Category]([Name])
values (null)
select [Id]
from [dbo].[Category]
where ##ROWCOUNT > 0 and [Id] = scope_identity()
I debugged the code right down to the AddObject method and verified that the Name property was set on the entity, but it doesn't reflect that in the SQL.
Can you see anything wrong with what I'm doing?
Updated with mapping xml
<?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="ProductCatalogModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
<EntityContainer Name="ProductCatalogModelStoreContainer">
<EntitySet Name="Category" EntityType="ProductCatalogModel.Store.Category" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Product" EntityType="ProductCatalogModel.Store.Product" store:Type="Tables" Schema="dbo" />
<AssociationSet Name="FK_Product_Category" Association="ProductCatalogModel.Store.FK_Product_Category">
<End Role="Category" EntitySet="Category" />
<End Role="Product" EntitySet="Product" />
</AssociationSet>
</EntityContainer>
<EntityType Name="Category">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Name" Type="varchar" Nullable="false" MaxLength="50" />
</EntityType>
<EntityType Name="Product">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Name" Type="varchar" Nullable="false" MaxLength="50" />
<Property Name="CategoryId" Type="int" Nullable="false" />
</EntityType>
<Association Name="FK_Product_Category">
<End Role="Category" Type="ProductCatalogModel.Store.Category" Multiplicity="1" />
<End Role="Product" Type="ProductCatalogModel.Store.Product" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Category">
<PropertyRef Name="Id" />
</Principal>
<Dependent Role="Product">
<PropertyRef Name="CategoryId" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="ProductCatalogModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
<EntityContainer Name="ProductCatalogEntities">
<EntitySet Name="Category" EntityType="ProductCatalogModel.Category" />
<EntitySet Name="Product" EntityType="ProductCatalogModel.Product" />
<AssociationSet Name="FK_Product_Category" Association="ProductCatalogModel.FK_Product_Category">
<End Role="Category" EntitySet="Category" />
<End Role="Product" EntitySet="Product" />
</AssociationSet>
</EntityContainer>
<EntityType Name="Category">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Int32" Nullable="false" />
<Property Name="Name" Type="String" Nullable="false" MaxLength="50" Unicode="false" FixedLength="false" />
<NavigationProperty Name="Product" Relationship="ProductCatalogModel.FK_Product_Category" FromRole="Category" ToRole="Product" />
</EntityType>
<EntityType Name="Product" Abstract="false">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Int32" Nullable="false" />
<Property Name="Name" Type="String" Nullable="false" MaxLength="50" Unicode="false" FixedLength="false" />
<NavigationProperty Name="Category" Relationship="ProductCatalogModel.FK_Product_Category" FromRole="Product" ToRole="Category" />
</EntityType>
<Association Name="FK_Product_Category">
<End Role="Category" Type="ProductCatalogModel.Category" Multiplicity="1" />
<End Role="Product" Type="ProductCatalogModel.Product" Multiplicity="*" />
</Association>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
<EntityContainerMapping StorageEntityContainer="ProductCatalogModelStoreContainer" CdmEntityContainer="ProductCatalogEntities">
<EntitySetMapping Name="Category">
<EntityTypeMapping TypeName="IsTypeOf(ProductCatalogModel.Category)">
<MappingFragment StoreEntitySet="Category">
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="Name" ColumnName="Name" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
<EntitySetMapping Name="Product">
<EntityTypeMapping TypeName="IsTypeOf(ProductCatalogModel.Product)">
<MappingFragment StoreEntitySet="Product">
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="Name" ColumnName="Name" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
<AssociationSetMapping Name="FK_Product_Category" TypeName="ProductCatalogModel.FK_Product_Category" StoreEntitySet="Product">
<EndProperty Name="Category">
<ScalarProperty Name="Id" ColumnName="CategoryId" />
</EndProperty>
<EndProperty Name="Product">
<ScalarProperty Name="Id" ColumnName="Id" />
</EndProperty>
</AssociationSetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
</DesignerInfoPropertySet>
</edmx:Connection>
<edmx:Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true" />
</DesignerInfoPropertySet>
</edmx:Options>
<!-- Diagram content (shape and connector positions) -->
<edmx:Diagrams>
<Diagram Name="Model">
<EntityTypeShape EntityType="ProductCatalogModel.Category" Width="1.5" PointX="0.75" PointY="0.875" Height="1.427958984375" IsExpanded="true" />
<EntityTypeShape EntityType="ProductCatalogModel.Product" Width="1.5" PointX="3" PointY="0.875" Height="1.427958984375" IsExpanded="true" />
<AssociationConnector Association="ProductCatalogModel.FK_Product_Category" ManuallyRouted="false">
<ConnectorPoint PointX="2.25" PointY="1.5889794921875" />
<ConnectorPoint PointX="3" PointY="1.5889794921875" /></AssociationConnector>
</Diagram></edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>
I hate to say this, but all I did was mess with it for about an hour and now it works. I did not change a freaking thing. Thanks Microsoft for such a production ready technology.
Probably not related to this issue but a couple of things to think about with EntityFramework
The compilation is database specific. Problems can arise if you don't compile it against the database version you plan on using (eg. SQL Server 2005 vs 2000) - this can cause issues if your local DB is 2005 vs a test or production enviroment is 2000.
Making a small change to the edmx file, even something as small as moving an entity in the designer view will cause the database mapping files to recompile and can possibly fix issues related to mapping / etc.

Entity Framework TPH with multiple abstract inheritance

I'm trying to do a Table Per Hierarchy model in Entity Framework(VS 2008 sp1, 3.5).
Most of my models have been very simple, an abstract type with multiple sub-types that inherit from it.
However, I've been struggling with this latest challenge. I have STUDENTS that I'd like to inherit from PERSONS(abstract) that should inherity from PARTIES(abstract).
Every time I do this I get a "Error 2078: The EntityType 'Model.PERSONS' is Abstract and can be mapped only using IsTypeOf." I guess the problem is PARTIES is already defined as IsTypeOf in the entity set.
So is this even possible? I can work around it by making PERSONS abstract = false and assigning a bogus conditional mapping. But this seems like a silly workaround.
Edit the model using XML Editor: find the mapping and add IsTypeOf to the corresponding EntityTypeMapping tag.
Here is an example resembling your case:
SQL Server DDL:
CREATE TABLE [dbo].[Student] (
[Id] [int] IDENTITY(1,1) NOT NULL,
[PartyInfo] [varchar](max) NOT NULL,
[PersonInfo] [varchar](max) NOT NULL,
[StudInfo] [varchar](max) NOT NULL,
CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED ( [Id] ASC )
)
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="test_1Model.Store" Alias="Self"
Provider="System.Data.SqlClient" ProviderManifestToken="2005"
xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"
xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
<EntityContainer Name="test_1ModelStoreContainer">
<EntitySet Name="Student" EntityType="test_1Model.Store.Student"
store:Type="Tables" Schema="dbo" />
</EntityContainer>
<EntityType Name="Student">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="int" Nullable="false"
StoreGeneratedPattern="Identity" />
<Property Name="PartyInfo" Type="varchar(max)" Nullable="false" />
<Property Name="PersonInfo" Type="varchar(max)" Nullable="false" />
<Property Name="StudInfo" Type="varchar(max)" Nullable="false" />
</EntityType>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="test_1Model" Alias="Self"
xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
<EntityContainer Name="test_Entities">
<EntitySet Name="PartySet" EntityType="test_1Model.Party" />
</EntityContainer>
<EntityType Name="Party" Abstract="true">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Int32" Nullable="false" />
<Property Name="PartyInfo" Type="String" Nullable="false"
MaxLength="Max" Unicode="false" FixedLength="false" />
</EntityType>
<EntityType Name="Person" BaseType="test_1Model.Party" Abstract="true" >
<Property Name="PersonInfo" Type="String" Nullable="false" />
</EntityType>
<EntityType Name="Student" BaseType="test_1Model.Person" >
<Property Name="StudInfo" 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="test_1ModelStoreContainer"
CdmEntityContainer="test_Entities">
<EntitySetMapping Name="PartySet">
<EntityTypeMapping TypeName="IsTypeOf(test_1Model.Party)">
<MappingFragment StoreEntitySet="Student">
<ScalarProperty Name="PartyInfo" ColumnName="PartyInfo" />
<ScalarProperty Name="Id" ColumnName="Id" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="IsTypeOf(test_1Model.Person)">
<MappingFragment StoreEntitySet="Student">
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="PersonInfo" ColumnName="PersonInfo" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="test_1Model.Student">
<MappingFragment StoreEntitySet="Student">
<ScalarProperty Name="PartyInfo" ColumnName="PartyInfo" />
<ScalarProperty Name="PersonInfo" ColumnName="PersonInfo" />
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="StudInfo" ColumnName="StudInfo" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing"
Value="EmbedInOutputAssembly" />
</DesignerInfoPropertySet>
</edmx:Connection>
<edmx:Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true" />
</DesignerInfoPropertySet>
</edmx:Options>
<!-- Diagram content (shape and connector positions) -->
<edmx:Diagrams>
<Diagram Name="SqlServer_Model" />
</edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>

How do I get the value of the Discriminator Column from an Entity Framework Entity

I've got a project in written in the EDM. Does anyone know how to get the value of the discriminator column from an entity in a TPH inheritance tree? I can't add the property to the entity. Is there any other way to get the value?
Thanks,
Roy
There is a simple way to solve the problem for the DBMS that suports updatable views.
You can simply create a view or a Defining Query having an additional discriminator column.
The original column should be mapped to the class property. In case when DBMS does not support the updatable views you can use Defining Query and then map stored procedures for Insert/Update/Delete operations.
Here is an example for Oracle database:
SQL:
CREATE TABLE TEST.TPH_TABLE (
ID NUMBER(9),
COLUMN_A VARCHAR2(20),
COLUMN_B VARCHAR2(20),
BASE_COLUMN VARCHAR2(20),
CONSTRAINT PK_TPH_TABLE PRIMARY KEY (ID)
);
EDMX:
<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="Model1.Store" Alias="Self" Provider="Devart.Data.Oracle" ProviderManifestToken="ORA" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
<EntityContainer Name="Model1StoreContainer">
<EntitySet Name="TPH_TABLE" EntityType="Model1.Store.TPH_TABLE" >
<DefiningQuery>
SELECT ID, BASE_COLUMN, COLUMN_A, COLUMN_B,
CASE WHEN COLUMN_A IS NOT NULL THEN '1' ELSE NULL END AS "IS_TABLE_A"
FROM TPH_TABLE
</DefiningQuery>
</EntitySet>
</EntityContainer>
<EntityType Name="TPH_TABLE">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Name="ID" Type="int" Nullable="false" />
<Property Name="BASE_COLUMN" Type="VARCHAR2" MaxLength="20" />
<Property Name="COLUMN_A" Type="VARCHAR2" MaxLength="20" />
<Property Name="COLUMN_B" Type="VARCHAR2" MaxLength="20" />
<Property Name="IS_TABLE_A" Type="VARCHAR2" MaxLength="1" />
</EntityType>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="Model1" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
<EntityContainer Name="Entities">
<EntitySet Name="TPH_TABLE" EntityType="Model1.TPH_TABLE" />
</EntityContainer>
<EntityType Name="TPH_TABLE" Abstract="true">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Name="ID" Type="Int32" Nullable="false" />
<Property Name="BASE_COLUMN" Type="String" MaxLength="20" Unicode="true" FixedLength="false" />
</EntityType>
<EntityType Name="TABLE_A" BaseType="Model1.TPH_TABLE" >
<Property Name="COLUMN_A" Type="String" Nullable="true" />
</EntityType>
<EntityType Name="TABLE_B" BaseType="Model1.TPH_TABLE" >
<Property Name="COLUMN_B" Type="String" Nullable="true" />
</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="Model1StoreContainer" CdmEntityContainer="Entities">
<EntitySetMapping Name="TPH_TABLE">
<EntityTypeMapping TypeName="Model1.TABLE_A">
<MappingFragment StoreEntitySet="TPH_TABLE">
<ScalarProperty Name="ID" ColumnName="ID" />
<ScalarProperty Name="BASE_COLUMN" ColumnName="BASE_COLUMN" />
<ScalarProperty Name="COLUMN_A" ColumnName="COLUMN_A" />
<Condition ColumnName="IS_TABLE_A" Value="1" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="Model1.TABLE_B">
<MappingFragment StoreEntitySet="TPH_TABLE">
<ScalarProperty Name="ID" ColumnName="ID" />
<ScalarProperty Name="BASE_COLUMN" ColumnName="BASE_COLUMN" />
<ScalarProperty Name="COLUMN_B" ColumnName="COLUMN_B" />
<Condition ColumnName="IS_TABLE_A" IsNull="true" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
</DesignerInfoPropertySet>
</edmx:Connection>
<edmx:Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true" />
</DesignerInfoPropertySet>
</edmx:Options>
<!-- Diagram content (shape and connector positions) -->
<edmx:Diagrams>
<Diagram Name="Model4">
<EntityTypeShape EntityType="Model1.TPH_TABLE" Width="1.5" PointX="3" PointY="0.5" Height="1.2636116536458335" IsExpanded="true" />
<EntityTypeShape EntityType="Model1.TABLE_A" Width="1.5" PointX="1.75" PointY="2.75" Height="1.099264322916667" />
<EntityTypeShape EntityType="Model1.TABLE_B" Width="1.5" PointX="4.25" PointY="2.75" Height="1.099264322916667" />
<InheritanceConnector EntityType="Model1.TABLE_B" ManuallyRouted="false">
<ConnectorPoint PointX="4.375" PointY="1.7636116536458335" />
<ConnectorPoint PointX="4.375" PointY="2.75" />
</InheritanceConnector>
<InheritanceConnector EntityType="Model1.TABLE_A" ManuallyRouted="false">
<ConnectorPoint PointX="3.125" PointY="1.7636116536458335" />
<ConnectorPoint PointX="3.125" PointY="2.75" />
</InheritanceConnector></Diagram></edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>