NKCSS
NKCSS

Reputation: 2746

Entity Framework Add Mapping to View

Our app. has a complex set of security rules.

I've implemented the rules in a SQL Statement, then translated it to Linq to SQL and the Entity Framework. the EF one was having some performance issues due to the nature of follow-up queries that have to take place on the data set, so I figured it would be best to just have the filtering done in SQL where I have maximum control over the statement that is executed, to ensure performance. I fit it in a View, thinking I'd let EF use that as a basis & continue the rest of the filtering there, but I am having trouble mapping the existing tables to the view.

The View

As you can see on the screenshot, all three columns are identified as Entity Key. The problem is that I can't define a FK for a View, so EF doesn't know how the connections are to be made. When I try to manualy create them, EF complains I need to specify a connection to all three columns (for a single relation), which is not possible. I've also tried it from the other end, but no dice.

Here's the .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>

Upvotes: 3

Views: 1563

Answers (1)

Santiago Regojo
Santiago Regojo

Reputation: 405

In the vwEntitiesFilter just click the field id, right click properties and in the properties window set "Entity Key" = false Then in toolbox, chose association and associate user.id field with vwEntitiesFilter.user_id and entity.id with vwEntitiesFilter.entity_id. This just allow you to make the relations

Upvotes: 1

Related Questions