广告
返回顶部
首页 > 资讯 > 精选 >Entity Framework中如何使用DataBase First模式实现增删改查
  • 509
分享到

Entity Framework中如何使用DataBase First模式实现增删改查

2023-06-29 10:06:06 509人浏览 八月长安
摘要

本篇内容主要讲解“Entity Framework中如何使用DataBase First模式实现增删改查”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Entity 

本篇内容主要讲解“Entity Framework中如何使用DataBase First模式实现增删改查”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Entity Framework中如何使用DataBase First模式实现增删改查”吧!

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

Entity Framework中如何使用DataBase First模式实现增删改查

然后点击添加

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

Entity Framework中如何使用DataBase First模式实现增删改查

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

Entity Framework中如何使用DataBase First模式实现增删改查

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

Entity Framework中如何使用DataBase First模式实现增删改查

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

Entity Framework中如何使用DataBase First模式实现增删改查

六、查看生成的项目结构

Entity Framework中如何使用DataBase First模式实现增删改查

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

Entity Framework中如何使用DataBase First模式实现增删改查

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

Entity Framework中如何使用DataBase First模式实现增删改查

查看生成的配置文件:

Entity Framework中如何使用DataBase First模式实现增删改查

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

七、查看生成的edmx文件

1、查看.tt下面的文件

Entity Framework中如何使用DataBase First模式实现增删改查

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

2、查看.edmx文件

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

Entity Framework中如何使用DataBase First模式实现增删改查

文件的整个结构如下:

<?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定义的是数据库表的结构:

Entity Framework中如何使用DataBase First模式实现增删改查

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

Entity Framework中如何使用DataBase First模式实现增删改查

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

Entity Framework中如何使用DataBase First模式实现增删改查

从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模式实现增删改查”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

--结束END--

本文标题: Entity Framework中如何使用DataBase First模式实现增删改查

本文链接: https://www.lsjlt.com/news/324284.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作