Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MappingHelper.cs for Type Hierarchies #11

Merged
merged 2 commits into from Aug 31, 2014

Conversation

dahrnsbrak
Copy link
Contributor

When using type hierarchies in an EF project, the TypeName is not exactly the ElementType.FullName, but rather OfType(ElementType.FullName).
For example:

<EntitySetMapping Name="People">
  <EntityTypeMapping TypeName="IsTypeOf(SchoolModel.Person)">
    <MappingFragment StoreEntitySet="Person">
      <ScalarProperty Name="PersonID" ColumnName="PersonID" />
      <ScalarProperty Name="FirstName" ColumnName="FirstName" />
      <ScalarProperty Name="LastName" ColumnName="LastName" />
    </MappingFragment>
  </EntityTypeMapping>
  <EntityTypeMapping TypeName="IsTypeOf(SchoolModel.Instructor)">
    <MappingFragment StoreEntitySet="Person">
      <ScalarProperty Name="PersonID" ColumnName="PersonID" />
      <ScalarProperty Name="HireDate" ColumnName="HireDate" />
      <Condition ColumnName="HireDate" IsNull="false" />
      <Condition ColumnName="EnrollmentDate" IsNull="true" />
    </MappingFragment>
  </EntityTypeMapping>
  <EntityTypeMapping TypeName="IsTypeOf(SchoolModel.Student)">
    <MappingFragment StoreEntitySet="Person">
      <ScalarProperty Name="PersonID" ColumnName="PersonID" />
      <ScalarProperty Name="EnrollmentDate" 
                      ColumnName="EnrollmentDate" />
      <Condition ColumnName="EnrollmentDate" IsNull="false" />
      <Condition ColumnName="HireDate" IsNull="true" />
    </MappingFragment>
  </EntityTypeMapping>
</EntitySetMapping>

You can see documentation here:
http://msdn.microsoft.com/en-us/library/vstudio/bb399272(v=vs.100).aspx

I've tested this change on my project which uses type hierarchies and it works fine. I did not do any performance checks with the new conditional.

When using type hierarchies in an EF project, the TypeName is not exactly the ElementType.FullName, but rather OfType(ElementType.FullName).  
For example:
<EntitySetMapping Name="People">
  <EntityTypeMapping TypeName="IsTypeOf(SchoolModel.Person)">
    <MappingFragment StoreEntitySet="Person">
      <ScalarProperty Name="PersonID" ColumnName="PersonID" />
      <ScalarProperty Name="FirstName" ColumnName="FirstName" />
      <ScalarProperty Name="LastName" ColumnName="LastName" />
    </MappingFragment>
  </EntityTypeMapping>
  <EntityTypeMapping TypeName="IsTypeOf(SchoolModel.Instructor)">
    <MappingFragment StoreEntitySet="Person">
      <ScalarProperty Name="PersonID" ColumnName="PersonID" />
      <ScalarProperty Name="HireDate" ColumnName="HireDate" />
      <Condition ColumnName="HireDate" IsNull="false" />
      <Condition ColumnName="EnrollmentDate" IsNull="true" />
    </MappingFragment>
  </EntityTypeMapping>
  <EntityTypeMapping TypeName="IsTypeOf(SchoolModel.Student)">
    <MappingFragment StoreEntitySet="Person">
      <ScalarProperty Name="PersonID" ColumnName="PersonID" />
      <ScalarProperty Name="EnrollmentDate" 
                      ColumnName="EnrollmentDate" />
      <Condition ColumnName="EnrollmentDate" IsNull="false" />
      <Condition ColumnName="HireDate" IsNull="true" />
    </MappingFragment>
  </EntityTypeMapping>
</EntitySetMapping>

You can see documentation here:
http://msdn.microsoft.com/en-us/library/vstudio/bb399272(v=vs.100).aspx

I've tested this change on my project which uses type hierarchies and it works fine.  I did not do any performance checks with the new conditional.
@MikaelEliasson
Copy link
Owner

Thank you very much! Do you think you can add a test for that?

I have a lot of stuff going on this weekend so I will probably merge this during the next week and publish a new version with this and a few other small fixes.

…s (you can only create ObjectSet of base type - note this will note work for deep inheritance, but does work in most simple cases). Added Type Hierarchy (Contact/Person) to test context and created test that fails before the MappingHelper fix.
@dahrnsbrak
Copy link
Contributor Author

I've made another commit with a test that fails without the fix I previously checked in. I also discovered that the EFQueryHelpers class fails with typed hierarchies. I put in a fix for that which uses reflection - I couldn't find a better way to handle this situation since you need to create the object set of the base type, not the derived type.

MikaelEliasson added a commit that referenced this pull request Aug 31, 2014
MappingHelper.cs for Type Hierarchies
@MikaelEliasson MikaelEliasson merged commit 407f2b7 into MikaelEliasson:master Aug 31, 2014
@MikaelEliasson
Copy link
Owner

Looks awesome. I doubt there will be any noticable performance hit as both changes are in parts of the code that shouldn't run many times.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants