Entity Framework Добавить сопоставление для просмотра - PullRequest
3 голосов
/ 19 сентября 2011

Наше приложение.имеет сложный набор правил безопасности.

Я реализовал правила в инструкции SQL, а затем перевел их на Linq для SQL и Entity Framework.в EF возникли некоторые проблемы с производительностью из-за характера последующих запросов, которые должны выполняться к набору данных, поэтому я решил, что было бы лучше просто выполнить фильтрацию в SQL, где у меня есть максимальный контроль над операторомэто выполняется, чтобы обеспечить производительность.Я помещаю его в представление, думая, что позволю EF использовать его в качестве основы и продолжить там остальную фильтрацию, но у меня возникают проблемы с отображением существующих таблиц в представление.

The View

Как видно на скриншоте, все три столбца обозначены как Entity Key.Проблема в том, что я не могу определить FK для View, поэтому EF не знает, как должны выполняться соединения.Когда я пытаюсь создать их вручную, EF жалуется, что мне нужно указать соединение со всеми тремя столбцами (для одного отношения), что невозможно.Я также попробовал это с другого конца, но без игры в кости.

Вот .edmx:

<?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="VPModel.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/2009/02/edm/ssdl">
        <EntityContainer Name="VPModelStoreContainer">
          <EntitySet Name="entity" EntityType="VPModel.Store.entity" store:Type="Tables" Schema="dbo" />
          <EntitySet Name="entity_type" EntityType="VPModel.Store.entity_type" store:Type="Tables" Schema="dbo" />
          <EntitySet Name="user" EntityType="VPModel.Store.user" store:Type="Tables" Schema="dbo" />
          <EntitySet Name="vwEntitiesFilteredOnSecurity" EntityType="VPModel.Store.vwEntitiesFilteredOnSecurity" store:Type="Views" store:Schema="dbo" store:Name="vwEntitiesFilteredOnSecurity">
            <DefiningQuery>SELECT 
      [vwEntitiesFilteredOnSecurity].[id] AS [id], 
      [vwEntitiesFilteredOnSecurity].[type_id] AS [type_id], 
      [vwEntitiesFilteredOnSecurity].[user_id] AS [user_id]
      FROM [dbo].[vwEntitiesFilteredOnSecurity] AS [vwEntitiesFilteredOnSecurity]</DefiningQuery>
          </EntitySet>
          <AssociationSet Name="FK_entity_entity" Association="VPModel.Store.FK_entity_entity">
            <End Role="entity" EntitySet="entity" />
            <End Role="entity1" EntitySet="entity" />
          </AssociationSet>
          <AssociationSet Name="FK_entity_entity_type" Association="VPModel.Store.FK_entity_entity_type">
            <End Role="entity_type" EntitySet="entity_type" />
            <End Role="entity" EntitySet="entity" />
          </AssociationSet>
          <AssociationSet Name="FK_entity_user" Association="VPModel.Store.FK_entity_user">
            <End Role="user" EntitySet="user" />
            <End Role="entity" EntitySet="entity" />
          </AssociationSet>
          <AssociationSet Name="FK_user_user" Association="VPModel.Store.FK_user_user">
            <End Role="user" EntitySet="user" />
            <End Role="user1" EntitySet="user" />
          </AssociationSet>
        </EntityContainer>
        <EntityType Name="entity">
          <Key>
            <PropertyRef Name="id" />
          </Key>
          <Property Name="id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
          <Property Name="name" Type="varchar" Nullable="false" MaxLength="200" />
          <Property Name="creator_id" Type="int" />
          <Property Name="type_id" Type="int" Nullable="false" />
          <Property Name="parent_id" Type="int" />
        </EntityType>
        <EntityType Name="entity_type">
          <Key>
            <PropertyRef Name="id" />
          </Key>
          <Property Name="id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
          <Property Name="name" Type="varchar" Nullable="false" MaxLength="150" />
        </EntityType>
        <EntityType Name="user">
          <Key>
            <PropertyRef Name="id" />
          </Key>
          <Property Name="id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
          <Property Name="name" Type="varchar" Nullable="false" MaxLength="250" />
        </EntityType>
        <!--Errors Found During Generation:
      warning 6002: The table/view 'mi7demo.dbo.vwEntitiesFilteredOnSecurity' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.
      -->
        <EntityType Name="vwEntitiesFilteredOnSecurity">
          <Key>
            <PropertyRef Name="id" />
            <PropertyRef Name="type_id" />
            <PropertyRef Name="user_id" />
          </Key>
          <Property Name="id" Type="int" Nullable="false" />
          <Property Name="type_id" Type="int" Nullable="false" />
          <Property Name="user_id" Type="int" Nullable="false" />
        </EntityType>
        <Association Name="FK_entity_entity">
          <End Role="entity" Type="VPModel.Store.entity" Multiplicity="0..1" />
          <End Role="entity1" Type="VPModel.Store.entity" Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="entity">
              <PropertyRef Name="id" />
            </Principal>
            <Dependent Role="entity1">
              <PropertyRef Name="parent_id" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
        <Association Name="FK_entity_entity_type">
          <End Role="entity_type" Type="VPModel.Store.entity_type" Multiplicity="1">
            <OnDelete Action="Cascade" />
          </End>
          <End Role="entity" Type="VPModel.Store.entity" Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="entity_type">
              <PropertyRef Name="id" />
            </Principal>
            <Dependent Role="entity">
              <PropertyRef Name="type_id" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
        <Association Name="FK_entity_user">
          <End Role="user" Type="VPModel.Store.user" Multiplicity="0..1" />
          <End Role="entity" Type="VPModel.Store.entity" Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="user">
              <PropertyRef Name="id" />
            </Principal>
            <Dependent Role="entity">
              <PropertyRef Name="creator_id" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
        <Association Name="FK_user_user">
          <End Role="user" Type="VPModel.Store.user" Multiplicity="1" />
          <End Role="user1" Type="VPModel.Store.user" Multiplicity="0..1" />
          <ReferentialConstraint>
            <Principal Role="user">
              <PropertyRef Name="id" />
            </Principal>
            <Dependent Role="user1">
              <PropertyRef Name="id" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
      </Schema>
    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema Namespace="VPModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
        <EntityContainer Name="VP" annotation:LazyLoadingEnabled="true">
          <EntitySet Name="entity" EntityType="VPModel.entity" />
          <EntitySet Name="entity_type" EntityType="VPModel.entity_type" />
          <EntitySet Name="user" EntityType="VPModel.user" />
          <EntitySet Name="vwEntitiesFilteredOnSecurity" EntityType="VPModel.vwEntitiesFilteredOnSecurity" />
          <AssociationSet Name="FK_entity_entity" Association="VPModel.FK_entity_entity">
            <End EntitySet="entity" Role="entity" />
            <End EntitySet="entity" Role="entity1" />
          </AssociationSet>
          <AssociationSet Name="FK_entity_entity_type" Association="VPModel.FK_entity_entity_type">
            <End Role="entity_type" EntitySet="entity_type" />
            <End Role="entity" EntitySet="entity" />
          </AssociationSet>
          <AssociationSet Name="FK_entity_user" Association="VPModel.FK_entity_user">
            <End Role="user" EntitySet="user" />
            <End Role="entity" EntitySet="entity" />
          </AssociationSet>
          <AssociationSet Name="FK_user_user" Association="VPModel.FK_user_user">
            <End Role="user" EntitySet="user" />
            <End Role="user1" EntitySet="user" />
          </AssociationSet>
        </EntityContainer>
        <EntityType Name="entity">
          <Key>
            <PropertyRef Name="id" />
          </Key>
          <Property Name="id" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
          <Property Name="name" Type="String" Nullable="false" MaxLength="200" Unicode="false" FixedLength="false" />
          <Property Name="creator_id" Type="Int32" />
          <Property Name="type_id" Type="Int32" Nullable="false" />
          <Property Name="parent_id" Type="Int32" />
          <NavigationProperty Name="entity1" Relationship="VPModel.FK_entity_entity" FromRole="entity" ToRole="entity1" />
          <NavigationProperty Name="entity2" Relationship="VPModel.FK_entity_entity" FromRole="entity1" ToRole="entity" />
          <NavigationProperty Name="entity_type" Relationship="VPModel.FK_entity_entity_type" FromRole="entity" ToRole="entity_type" />
          <NavigationProperty Name="user" Relationship="VPModel.FK_entity_user" FromRole="entity" ToRole="user" />
        </EntityType>
        <EntityType Name="entity_type">
          <Key>
            <PropertyRef Name="id" />
          </Key>
          <Property Name="id" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
          <Property Name="name" Type="String" Nullable="false" MaxLength="150" Unicode="false" FixedLength="false" />
          <NavigationProperty Name="entity" Relationship="VPModel.FK_entity_entity_type" FromRole="entity_type" ToRole="entity" />
        </EntityType>
        <EntityType Name="user">
          <Key>
            <PropertyRef Name="id" />
          </Key>
          <Property Name="id" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
          <Property Name="name" Type="String" Nullable="false" MaxLength="250" Unicode="false" FixedLength="false" />
          <NavigationProperty Name="entity" Relationship="VPModel.FK_entity_user" FromRole="user" ToRole="entity" />
          <NavigationProperty Name="user1" Relationship="VPModel.FK_user_user" FromRole="user" ToRole="user1" />
          <NavigationProperty Name="user2" Relationship="VPModel.FK_user_user" FromRole="user1" ToRole="user" />
        </EntityType>
        <EntityType Name="vwEntitiesFilteredOnSecurity">
          <Key>
            <PropertyRef Name="id" />
            <PropertyRef Name="type_id" />
            <PropertyRef Name="user_id" />
          </Key>
          <Property Name="id" Type="Int32" Nullable="false" />
          <Property Name="type_id" Type="Int32" Nullable="false" />
          <Property Name="user_id" Type="Int32" Nullable="false" />
        </EntityType>
        <Association Name="FK_entity_entity">
          <End Type="VPModel.entity" Multiplicity="0..1" Role="entity" />
          <End Type="VPModel.entity" Multiplicity="*" Role="entity1" />
          <ReferentialConstraint>
            <Principal Role="entity">
              <PropertyRef Name="id" />
            </Principal>
            <Dependent Role="entity1">
              <PropertyRef Name="parent_id" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
        <Association Name="FK_entity_entity_type">
          <End Role="entity_type" Type="VPModel.entity_type" Multiplicity="1">
            <OnDelete Action="Cascade" />
          </End>
          <End Role="entity" Type="VPModel.entity" Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="entity_type">
              <PropertyRef Name="id" />
            </Principal>
            <Dependent Role="entity">
              <PropertyRef Name="type_id" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
        <Association Name="FK_entity_user">
          <End Role="user" Type="VPModel.user" Multiplicity="0..1" />
          <End Role="entity" Type="VPModel.entity" Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="user">
              <PropertyRef Name="id" />
            </Principal>
            <Dependent Role="entity">
              <PropertyRef Name="creator_id" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
        <Association Name="FK_user_user">
          <End Role="user" Type="VPModel.user" Multiplicity="1" />
          <End Role="user1" Type="VPModel.user" Multiplicity="0..1" />
          <ReferentialConstraint>
            <Principal Role="user">
              <PropertyRef Name="id" />
            </Principal>
            <Dependent Role="user1">
              <PropertyRef Name="id" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
      </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="VPModelStoreContainer" CdmEntityContainer="VP">
          <EntitySetMapping Name="entity"><EntityTypeMapping TypeName="VPModel.entity"><MappingFragment StoreEntitySet="entity">
            <ScalarProperty Name="id" ColumnName="id" />
            <ScalarProperty Name="name" ColumnName="name" />
            <ScalarProperty Name="creator_id" ColumnName="creator_id" />
            <ScalarProperty Name="type_id" ColumnName="type_id" />
            <ScalarProperty Name="parent_id" ColumnName="parent_id" />
          </MappingFragment></EntityTypeMapping></EntitySetMapping>
          <EntitySetMapping Name="entity_type"><EntityTypeMapping TypeName="VPModel.entity_type"><MappingFragment StoreEntitySet="entity_type">
            <ScalarProperty Name="id" ColumnName="id" />
            <ScalarProperty Name="name" ColumnName="name" />
          </MappingFragment></EntityTypeMapping></EntitySetMapping>
          <EntitySetMapping Name="user"><EntityTypeMapping TypeName="VPModel.user"><MappingFragment StoreEntitySet="user">
            <ScalarProperty Name="id" ColumnName="id" />
            <ScalarProperty Name="name" ColumnName="name" />
          </MappingFragment></EntityTypeMapping></EntitySetMapping>
          <EntitySetMapping Name="vwEntitiesFilteredOnSecurity"><EntityTypeMapping TypeName="VPModel.vwEntitiesFilteredOnSecurity"><MappingFragment StoreEntitySet="vwEntitiesFilteredOnSecurity">
            <ScalarProperty Name="id" ColumnName="id" />
            <ScalarProperty Name="type_id" ColumnName="type_id" />
            <ScalarProperty Name="user_id" ColumnName="user_id" />
          </MappingFragment></EntityTypeMapping></EntitySetMapping>
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>
  </edmx:Runtime>
  <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
  <Designer xmlns="http://schemas.microsoft.com/ado/2008/10/edmx">
    <Connection>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
      </DesignerInfoPropertySet>
    </Connection>
    <Options>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="ValidateOnBuild" Value="true" />
        <DesignerProperty Name="EnablePluralization" Value="False" />
        <DesignerProperty Name="IncludeForeignKeysInModel" Value="True" />
      </DesignerInfoPropertySet>
    </Options>
    <!-- Diagram content (shape and connector positions) -->
    <Diagrams>
      <Diagram Name="ViewProblem">
        <EntityTypeShape EntityType="VPModel.entity" Width="1.5" PointX="3" PointY="3.75" Height="2.7494921874999987" IsExpanded="true" />
        <EntityTypeShape EntityType="VPModel.entity_type" Width="1.5" PointX="0.75" PointY="5.875" Height="1.5956835937499996" IsExpanded="true" />
        <EntityTypeShape EntityType="VPModel.user" Width="1.5" PointX="0.75" PointY="0.75" Height="1.9802864583333331" IsExpanded="true" />
        <EntityTypeShape EntityType="VPModel.vwEntitiesFilteredOnSecurity" Width="1.5" PointX="5.75" PointY="3.25" Height="1.5956835937499996" IsExpanded="true" />
        <AssociationConnector ManuallyRouted="false" Association="VPModel.FK_entity_entity">
          <ConnectorPoint PointX="3.5319230769230767" PointY="6.4994921874999987" />
          <ConnectorPoint PointX="3.5319230769230767" PointY="6.7494921875" />
          <ConnectorPoint PointX="3.9784615384615383" PointY="6.7494921875" />
          <ConnectorPoint PointX="3.9784615384615383" PointY="6.4994921874999987" />
        </AssociationConnector>
        <AssociationConnector Association="VPModel.FK_entity_entity_type" ManuallyRouted="false">
          <ConnectorPoint PointX="2.25" PointY="6.18724609375" />
          <ConnectorPoint PointX="3" PointY="6.18724609375" />
        </AssociationConnector>
        <AssociationConnector Association="VPModel.FK_entity_user" ManuallyRouted="false">
          <ConnectorPoint PointX="2.25" PointY="1.7401432291666663" />
          <ConnectorPoint PointX="3.75" PointY="1.7401432291666663" />
          <ConnectorPoint PointX="3.75" PointY="3.75" />
        </AssociationConnector>
        <AssociationConnector Association="VPModel.FK_user_user" ManuallyRouted="false">
          <ConnectorPoint PointX="1.2819230769230767" PointY="2.7302864583333331" />
          <ConnectorPoint PointX="1.2819230769230767" PointY="2.9802864583333331" />
          <ConnectorPoint PointX="1.7284615384615383" PointY="2.9802864583333331" />
          <ConnectorPoint PointX="1.7284615384615383" PointY="2.7302864583333331" />
        </AssociationConnector>
      </Diagram>
    </Diagrams>
  </Designer>
</edmx:Edmx>

1 Ответ

1 голос
/ 20 сентября 2013

В vwEntitiesFilter просто щелкните идентификатор поля, щелкните правой кнопкой мыши свойства и в окне свойств установите «Ключ сущности» = false Затем в наборе инструментов выберите ассоциацию и свяжите поле user.id с vwEntitiesFilter.user_id и entity.id с vwEntitiesFilter.entity_id. Это просто позволит вам сделать отношения

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...