亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Entity?Framework生成DataBase?First模式

 更新時(shí)間:2022年03月08日 16:32:16   作者:.NET開發(fā)菜鳥  
本文詳細(xì)講解了Entity?Framework生成DataBase?First模式的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、新建控制臺(tái)應(yīng)用程序,然后右鍵->添加新建項(xiàng),選擇數(shù)據(jù)里面的實(shí)體數(shù)據(jù)模型:

然后點(diǎn)擊添加

二、選擇來自數(shù)據(jù)庫的EF設(shè)計(jì)器,并點(diǎn)擊下一步

三、在實(shí)體數(shù)據(jù)模型向?qū)Ы缑孢x擇要使用的數(shù)據(jù)連接,或者點(diǎn)擊新建連接按鈕創(chuàng)建新的連接,這里選擇已有的連接,并點(diǎn)擊下一步:

四、選擇實(shí)體框架6.0,點(diǎn)擊下一步:

五、選擇要操作的表,并點(diǎn)擊完成:

六、查看生成的項(xiàng)目結(jié)構(gòu)

自動(dòng)添加了EntityFramework的引用。同時(shí)會(huì)在項(xiàng)目的根目錄下面生成一個(gè)package文件夾:

package文件夾里面存放的是與EntityFramework有關(guān)的文件:

查看生成的配置文件:

ConfigSections節(jié)點(diǎn)里面有一個(gè)section段的name為entityFramework,下面有一個(gè)節(jié)點(diǎn)的名稱就是該section段name的值。如果想在其他地方使用EF的配置信息,那么需要把configSections、connectionStrings、entityFramework三個(gè)節(jié)點(diǎn)都需要拷貝過去,并且configSections節(jié)點(diǎn)是在所有節(jié)點(diǎn)的最前面。

七、查看生成的edmx文件

1、查看.tt下面的文件

.tt文件下面生成的就是數(shù)據(jù)庫表對(duì)應(yīng)的實(shí)體類,并且是但是形式的。

2、查看.edmx文件

在edmx文件上面右鍵選擇打開方式,選擇XML(文本)編輯器方式打開:

文件的整個(gè)結(jié)構(gòu)如下:

<?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定義的是數(shù)據(jù)庫表的結(jié)構(gòu):

CSDL content定義的是實(shí)體類的結(jié)構(gòu):

C-S mapping content定義的是實(shí)體類和數(shù)據(jù)庫表的映射關(guān)系:

從C-S mapping content節(jié)點(diǎn)中可以看出,EF為什么會(huì)自動(dòng)生成實(shí)體類,或者通過實(shí)體類為什么能操作數(shù)據(jù)庫的數(shù)據(jù)了。

從這里可以看出EF做的第一件事情:取數(shù)據(jù)庫的數(shù)據(jù)表結(jié)構(gòu),生成實(shí)體類,在表和實(shí)體之間自動(dòng)建立映射關(guān)系。

3、StudentManager.tt是T4模板,是用來生成實(shí)體類的模板。

T4模板:從edmx中取數(shù)據(jù)結(jié)構(gòu),按照模板生成對(duì)應(yīng)格式的實(shí)體類。

4、StudentManager.Context.cs文件

//------------------------------------------------------------------------------
// <auto-generated>
//     此代碼已從模板生成。
//
//     手動(dòng)更改此文件可能導(dǎo)致應(yīng)用程序出現(xiàn)意外的行為。
//     如果重新生成代碼,將覆蓋對(duì)此文件的手動(dòng)更改。
// </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,用來操作數(shù)據(jù)庫。

到此這篇關(guān)于Entity Framework生成DataBase First模式的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論