zoukankan      html  css  js  c++  java
  • Entity Framework应用:EntityFramework DataBase First模式

    在这篇文章中讲解如何使用EF的DbFirst模式实现数据的增删改查

    一、新建控制台应用程序,然后右键->添加新建项,选择数据里面的实体数据模型:

    然后点击添加

    二、选择来自数据库的EF设计器,并点击下一步

    三、在实体数据模型向导界面选择要使用的数据连接,或者点击新建连接按钮创建新的连接,这里选择已有的连接,并点击下一步:

    四、选择实体框架6.0,点击下一步:

    五、选择要操作的表,并点击完成:

    六、查看生成的项目结构

    自动添加了EntityFramework的引用。同时会在项目的根目录下面生成一个package文件夹:

    package文件夹里面存放的是与EntityFramework有关的文件:

    查看生成的配置文件:

    ConfigSections节点里面有一个section段的name为entityFramework,下面有一个节点的名称就是该section段name的值。如果想在其他地方使用EF的配置信息,那么需要把configSections、connectionStrings、entityFramework三个节点都需要拷贝过去,并且configSections节点是在所有节点的最前面。

    七、查看生成的edmx文件

    1、查看.tt下面的文件

    .tt文件下面生成的就是数据库表对应的实体类,并且是但是形式的。

    2、查看.edmx文件

    在edmx文件上面右键选择打开方式,选择XML(文本)编辑器方式打开:

    文件的整个结构如下:

    <?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="StudentSystemModel.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="Student">
              <Key>
                <PropertyRef Name="StudentID" />
              </Key>
              <Property Name="StudentID" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
              <Property Name="StudentName" Type="varchar" MaxLength="16" />
              <Property Name="Sex" Type="varchar" MaxLength="8" />
              <Property Name="Age" Type="int" />
              <Property Name="Major" Type="varchar" MaxLength="32" />
              <Property Name="Email" Type="varchar" MaxLength="32" />
            </EntityType>
            <EntityContainer Name="StudentSystemModelStoreContainer">
              <EntitySet Name="Student" EntityType="Self.Student" Schema="dbo" store:Type="Tables" />
            </EntityContainer>
          </Schema>
        </edmx:StorageModels>
        <!-- CSDL content -->
        <edmx:ConceptualModels>
          <Schema Namespace="StudentSystemModel" 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="Student">
              <Key>
                <PropertyRef Name="StudentID" />
              </Key>
              <Property Name="StudentID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
              <Property Name="StudentName" Type="String" MaxLength="16" FixedLength="false" Unicode="false" />
              <Property Name="Sex" Type="String" MaxLength="8" FixedLength="false" Unicode="false" />
              <Property Name="Age" Type="Int32" />
              <Property Name="Major" Type="String" MaxLength="32" FixedLength="false" Unicode="false" />
              <Property Name="Email" Type="String" MaxLength="32" FixedLength="false" Unicode="false" />
            </EntityType>
            <EntityContainer Name="StudentSystemEntities" annotation:LazyLoadingEnabled="true">
              <EntitySet Name="Students" EntityType="Self.Student" />
            </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="StudentSystemModelStoreContainer" CdmEntityContainer="StudentSystemEntities">
              <EntitySetMapping Name="Students">
                <EntityTypeMapping TypeName="StudentSystemModel.Student">
                  <MappingFragment StoreEntitySet="Student">
                    <ScalarProperty Name="StudentID" ColumnName="StudentID" />
                    <ScalarProperty Name="StudentName" ColumnName="StudentName" />
                    <ScalarProperty Name="Sex" ColumnName="Sex" />
                    <ScalarProperty Name="Age" ColumnName="Age" />
                    <ScalarProperty Name="Major" ColumnName="Major" />
                    <ScalarProperty Name="Email" ColumnName="Email" />
                  </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="true" />
            <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>

    其中SSDL content定义的是数据库表的结构:

    CSDL content定义的是实体类的结构:

    C-S mapping content定义的是实体类和数据库表的映射关系:

    从C-S mapping content节点中可以看出,EF为什么会自动生成实体类,或者通过实体类为什么能操作数据库的数据了。

    从这里可以看出EF做的第一件事情:取数据库的数据表结构,生成实体类,在表和实体之间自动建立映射关系。

    3、StudentManager.tt是T4模板,是用来生成实体类的模板。

    T4模板:从edmx中取数据结构,按照模板生成对应格式的实体类。

    4、StudentManager.Context.cs文件

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     此代码已从模板生成。
    //
    //     手动更改此文件可能导致应用程序出现意外的行为。
    //     如果重新生成代码,将覆盖对此文件的手动更改。
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    namespace EFDbFirstDemo
    {
        using System;
        using System.Data.Entity;
        using System.Data.Entity.Infrastructure;
        
        public partial class StudentSystemEntities : DbContext
        {
            public StudentSystemEntities()
                : base("name=StudentSystemEntities")
            {
            }
        
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                throw new UnintentionalCodeFirstException();
            }
        
            public virtual DbSet<Student> Students { get; set; }
        }
    }

    StudentSystemEntities继承自DbContext,用来操作数据库。

  • 相关阅读:
    PHP面向对象魔术方法基本了解
    PHP面向对象访问修饰符的基本了解
    php析构函数小结
    php构造方法(函数)基础
    php面向对象成员方法(函数)练习
    php面向对象的初认识
    用JS把数组内的日期转换为星期
    JavaScript事件(随笔)
    Spring框架的核心功能之AOP技术
    Spring框架 IOC注解
  • 原文地址:https://www.cnblogs.com/dotnet261010/p/9080540.html
Copyright © 2011-2022 走看看