Entity Framework生成DataBase First模式

 更新时间:2022年03月08日 16:32:16   作者:.NET开发菜鸟  
本文详细讲解了Entity Framework生成DataBase First模式的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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

然后点击添加

二、选择来自数据库的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,用来操作数据库。

到此这篇关于Entity Framework生成DataBase First模式的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • .Net整合Json实现REST服务客户端的方法详解

    .Net整合Json实现REST服务客户端的方法详解

    这篇文章主要给大家介绍了关于.Net整合Json实现REST服务客户端的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-01-01
  • ASP.NET MVC中将控制器分离到类库的实现

    ASP.NET MVC中将控制器分离到类库的实现

    这篇文章主要介绍了ASP.NET MVC中将控制器分离到类库的实现的相关资料,需要的朋友可以参考下
    2015-06-06
  • ASP.NET中实现定制自己的委托和事件参数类

    ASP.NET中实现定制自己的委托和事件参数类

    这篇文章主要介绍了ASP.NET中实现定制自己的委托和事件参数类,需要的朋友可以参考下
    2014-08-08
  • Asp.Net Core 企业微信静默授权的实现

    Asp.Net Core 企业微信静默授权的实现

    这篇文章主要介绍了Asp.Net Core 企业微信静默授权的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • c#生成图片缩略图的类(2种实现思路)

    c#生成图片缩略图的类(2种实现思路)

    4个重载方法,有直接返回Image对象的,有生成缩略图,并且保存到指定目录的,具体祥看下文
    2013-05-05
  • jquery repeater 模仿 Google 展开页面预览子视图

    jquery repeater 模仿 Google 展开页面预览子视图

    节后的这一周, 希望大家能挺住, hehe, 这两天给大家准备一个 Repeater 子视图的例子, 模拟了 Google 搜索结果后的页面的预览, 其实也只是显示了一段问题
    2011-10-10
  • ASP.NET中Config文件的读写示例

    ASP.NET中Config文件的读写示例

    通常我们在.NET开发过程中,会接触二种类型的配置文件:config文件,xml文件,下面这篇文章主要给大家介绍了关于ASP.NET中Config文件读写的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-08-08
  • 页面间隔半秒钟更新时间 Asp.net使用Comet开发http长连接示例分享

    页面间隔半秒钟更新时间 Asp.net使用Comet开发http长连接示例分享

    Comet(Reverse AJAX)主要是通过HTTP长连接, 保持和服务器的连接,实现Server PUSH 和双向通信,下面通过示例学习他的使用方法
    2014-01-01
  • ASP.NET MVC学习之NuGet在VS中的运用浅谈

    ASP.NET MVC学习之NuGet在VS中的运用浅谈

    这篇文章主要给大家介绍了关于ASP.NET MVC学习之NuGet在VS中运用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧。
    2018-01-01
  • asp.net获取真实ip的方法

    asp.net获取真实ip的方法

    这篇文章主要介绍了asp.net获取真实ip的方法,通过一个简单的自定义函数达到获取用户真实IP的功能,非常简单实用,需要的朋友可以参考下
    2015-07-07

最新评论