从同一个表

时间:2017-06-01 13:16:49

标签: entity-framework entity-framework-6 ef-database-first

是否可以同时使用同一个表支持实体类型和多对多关联?我尝试了几种不同的方法,它们都因不同的错误而失败。

首先,我尝试从数据库创建模型,然后在设计器中创建关联。enter image description here

它为我提供了正确的FoosBars导航属性,但它没有任何表支持,并且在我尝试执行查询时失败。

Model1.msl(3,4) : error 3027: No mapping specified for the following EntitySet/AssociationSet - BarFoo.

所以我尝试将关联映射到设计器中的FooBar表。只有FooREFBarREF是关联的一部分,但EF也希望映射主键。

ERROR (3025): Problem in Mapping Fragment starting at line 32: Must specify mapping for all key properties (FooBar.ObjectID) of table FooBar.

由于我无法映射ObjectID和RowVersion,我尝试使用edmx中的QueryView来排除它们。

<edmx:Mappings>
      <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
        <EntityContainerMapping StorageEntityContainer="TestModelStoreContainer" CdmEntityContainer="TestEntities">
          <EntitySetMapping Name="Bars">
            <EntityTypeMapping TypeName="TestModel.Bar">
              <MappingFragment StoreEntitySet="Bar">
                <ScalarProperty Name="ObjectID" ColumnName="ObjectID" />
                <ScalarProperty Name="Count" ColumnName="Count" />
                <ScalarProperty Name="RowVersion" ColumnName="RowVersion" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
          <EntitySetMapping Name="Foos">
            <EntityTypeMapping TypeName="TestModel.Foo">
              <MappingFragment StoreEntitySet="Foo">
                <ScalarProperty Name="ObjectID" ColumnName="ObjectID" />
                <ScalarProperty Name="Name" ColumnName="Name" />
                <ScalarProperty Name="RowVersion" ColumnName="RowVersion" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
          <EntitySetMapping Name="FooBars">
            <EntityTypeMapping TypeName="TestModel.FooBar">
              <MappingFragment StoreEntitySet="FooBar">
                <ScalarProperty Name="ObjectID" ColumnName="ObjectID" />
                <ScalarProperty Name="RowVersion" ColumnName="RowVersion" />
                <ScalarProperty Name="BarREF" ColumnName="BarREF" />
                <ScalarProperty Name="FooREF" ColumnName="FooREF" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
          <AssociationSetMapping Name="BarFoo" TypeName="TestModel.BarFoo" >
            <EndProperty Name="Foo">
              <ScalarProperty Name="ObjectID" ColumnName="FooREF" />
            </EndProperty>
            <EndProperty Name="Bar">
              <ScalarProperty Name="ObjectID" ColumnName="BarREF" />
            </EndProperty>
            <QueryView>
              SELECT VALUE TestModel.BarFoo(CREATEREF(TestEntities.Foos, row(f.FooREF), TestModel.Foo), CREATEREF(TestEntities.Bars, row(f.BarREF), TestModel.Bar))
              FROM TestModelStoreContainer.FooBar AS f
            </QueryView>
          </AssociationSetMapping>
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>

但是,尽管记录了QueryView元素,但它的添加会导致架构验证失败。

MappingException: Schema specified is not valid. Errors: 
<File Unknown>(39,8) : error 2025: XML Schema validation failed for mapping schema. Schema Error Information : The element 'AssociationSetMapping' in namespace 'http://schemas.microsoft.com/ado/2009/11/mapping/cs' has invalid child element 'QueryView' in namespace 'http://schemas.microsoft.com/ado/2009/11/mapping/cs'. List of possible elements expected: 'Condition, ModificationFunctionMapping' in namespace 'http://schemas.microsoft.com/ado/2009/11/mapping/cs'..

我在这里缺少一个策略吗?最后,我希望能够将FooBar视为一个实体,同时保留FoosBars导航属性。这可能吗?

0 个答案:

没有答案