添加定义查询

 

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
      <Schema Namespace="SchoolModel.Store" Provider="System.Data.SqlClient" ProviderManifestToken="2012" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
     
        <!--数据库模型-->
        <EntityType Name="GradeReport">
          <Key>
            <PropertyRef Name="CourseID" />
            <PropertyRef Name="FirstName" />
            <PropertyRef Name="LastName" />
          </Key>
          <Property Name="CourseID"
                    Type="int"
                    Nullable="false" />
          <Property Name="Grade"
                    Type="decimal"
                    Precision="3"
                    Scale="2" />
          <Property Name="FirstName"
                    Type="nvarchar"
                    Nullable="false"
                    MaxLength="50" />
          <Property Name="LastName"
                    Type="nvarchar"
                    Nullable="false"
                    MaxLength="50" />
        </EntityType>     
        <EntityContainer Name="SchoolModelStoreContainer">
          <EntitySet Name="GradeReport" EntityType="Self.GradeReport" >
            <DefiningQuery>
              SELECT CourseID, Grade, FirstName, LastName
              FROM StudentGrade
              JOIN
              (SELECT * FROM Person WHERE EnrollmentDate IS NOT NULL) AS p
              ON StudentID = p.PersonID
            </DefiningQuery>
          </EntitySet>
          <EntitySet Name="Course" EntityType="Self.Course" Schema="dbo" store:Type="Tables" />
          <EntitySet Name="CourseInstructor" EntityType="Self.CourseInstructor" Schema="dbo" store:Type="Tables" />
          <EntitySet Name="Department" EntityType="Self.Department" Schema="dbo" store:Type="Tables" />
          <EntitySet Name="OfficeAssignment" EntityType="Self.OfficeAssignment" Schema="dbo" store:Type="Tables" />
          <EntitySet Name="OnlineCourse" EntityType="Self.OnlineCourse" Schema="dbo" store:Type="Tables" />
          <EntitySet Name="OnsiteCourse" EntityType="Self.OnsiteCourse" Schema="dbo" store:Type="Tables" />
          <EntitySet Name="Person" EntityType="Self.Person" Schema="dbo" store:Type="Tables" />
          <EntitySet Name="StudentGrade" EntityType="Self.StudentGrade" Schema="dbo" store:Type="Tables" />
          <AssociationSet Name="FK_Course_Department" Association="Self.FK_Course_Department">
            <End Role="Department" EntitySet="Department" />
            <End Role="Course" EntitySet="Course" />
          </AssociationSet>
          <AssociationSet Name="FK_CourseInstructor_Course" Association="Self.FK_CourseInstructor_Course">
            <End Role="Course" EntitySet="Course" />
            <End Role="CourseInstructor" EntitySet="CourseInstructor" />
          </AssociationSet>
          <AssociationSet Name="FK_CourseInstructor_Person" Association="Self.FK_CourseInstructor_Person">
            <End Role="Person" EntitySet="Person" />
            <End Role="CourseInstructor" EntitySet="CourseInstructor" />
          </AssociationSet>
          <AssociationSet Name="FK_OfficeAssignment_Person" Association="Self.FK_OfficeAssignment_Person">
            <End Role="Person" EntitySet="Person" />
            <End Role="OfficeAssignment" EntitySet="OfficeAssignment" />
          </AssociationSet>
          <AssociationSet Name="FK_OnlineCourse_Course" Association="Self.FK_OnlineCourse_Course">
            <End Role="Course" EntitySet="Course" />
            <End Role="OnlineCourse" EntitySet="OnlineCourse" />
          </AssociationSet>
          <AssociationSet Name="FK_OnsiteCourse_Course" Association="Self.FK_OnsiteCourse_Course">
            <End Role="Course" EntitySet="Course" />
            <End Role="OnsiteCourse" EntitySet="OnsiteCourse" />
          </AssociationSet>
          <AssociationSet Name="FK_StudentGrade_Course" Association="Self.FK_StudentGrade_Course">
            <End Role="Course" EntitySet="Course" />
            <End Role="StudentGrade" EntitySet="StudentGrade" />
          </AssociationSet>
          <AssociationSet Name="FK_StudentGrade_Student" Association="Self.FK_StudentGrade_Student">
            <End Role="Person" EntitySet="Person" />
            <End Role="StudentGrade" EntitySet="StudentGrade" />
          </AssociationSet>
        </EntityContainer>
      </Schema>
    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema Namespace="SchoolModel" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
     
        <!--概念模型-->        
        <EntityType Name="GradeReport">
          <Key>
            <PropertyRef Name="CourseID" />
            <PropertyRef Name="FirstName" />
            <PropertyRef Name="LastName" />
          </Key>
          <Property Name="CourseID"
                    Type="Int32"
                    Nullable="false" />
          <Property Name="Grade"
                    Type="Decimal"
                    Precision="3"
                    Scale="2" />
          <Property Name="FirstName"
                    Type="String"
                    Nullable="false"
                    MaxLength="50" />
          <Property Name="LastName"
                    Type="String"
                    Nullable="false"
                    MaxLength="50" />
        </EntityType>
        <EntityContainer Name="SchoolEntities" annotation:LazyLoadingEnabled="true">          
          <EntitySet Name="GradeReport" EntityType="Self.GradeReport" />      

        </EntityContainer>
      </Schema>
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
      <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
        <EntityContainerMapping StorageEntityContainer="SchoolModelStoreContainer" CdmEntityContainer="SchoolEntities">
         
          <!--映射(概念模型与数据库模型)-->
          <EntitySetMapping Name="GradeReport">
            <EntityTypeMapping TypeName="IsTypeOf(SchoolModel.GradeReport)">
              <MappingFragment StoreEntitySet="GradeReport">
                <ScalarProperty Name="CourseID" ColumnName="CourseID" />
                <ScalarProperty Name="Grade" ColumnName="Grade" />
                <ScalarProperty Name="FirstName" ColumnName="FirstName" />
                <ScalarProperty Name="LastName" ColumnName="LastName" />
              </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/2009/11/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" />
        <DesignerProperty Name="UseLegacyProvider" Value="false" />
        <DesignerProperty Name="CodeGenerationStrategy" Value="" />
      </DesignerInfoPropertySet>
    </Options>
    <!-- Diagram content (shape and connector positions) -->
    <Diagrams></Diagrams>
  </Designer>
</edmx:Edmx>

 

执行自定义查询

using (var context = new SchoolEntities()) 
{ 
    var report = context.GradeReports.FirstOrDefault(); 
    Console.WriteLine("{0} {1} got {2}", 
        report.FirstName, report.LastName, report.Grade); 
}

 

参考:https://msdn.microsoft.com/zh-cn/library/cc982038.aspx

       https://msdn.microsoft.com/zh-cn/data/jj730307

posted @ 2015-07-19 16:31  senki  阅读(281)  评论(0编辑  收藏  举报