我们的应用。有一套复杂的安全规则。
我在SQL语句中实现了规则,然后将其转换为Linq to SQL和Entity Framework。由于必须在数据集上进行后续查询的性质,EF的一个性能问题,所以我认为最好只在SQL中完成过滤,我可以最大限度地控制语句执行,以确保性能。我把它放在一个视图中,以为我会让EF使用它作为基础&继续在那里进行剩余的过滤,但是我无法将现有表映射到视图。
正如您在屏幕截图中看到的,所有三列都被标识为实体密钥。问题是我无法为View定义FK,因此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>
答案 0 :(得分:1)
在vwEntitiesFilter中只需单击字段ID,右键单击属性,然后在属性窗口中设置“Entity Key”= false 然后在工具箱中,选择关联并将user.id字段与vwEntitiesFilter.user_id和entity.id与vwEntitiesFilter.entity_id相关联。 这只是让你建立关系