当前位置:Gxlcms > 数据库问题 > 《MVC+EF》——用DBFirst创建ADO.NET实体数据模型和对象关系映射

《MVC+EF》——用DBFirst创建ADO.NET实体数据模型和对象关系映射

时间:2021-07-01 10:21:17 帮助过:58人阅读

  •          <Key>  
  •            <PropertyRef Name="Id" />  
  •          </Key>  
  •          <Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />  
  •          <Property Name="Author" Type="int" Nullable="false" />  
  •          <Property Name="Name" Type="nvarchar" MaxLength="50" Nullable="false" />  
  •          <Property Name="Remark" Type="nvarchar" MaxLength="200" />  
  •          <Property Name="Statu" Type="int" Nullable="false" />  
  •          <Property Name="IsDel" Type="bit" Nullable="false" />  
  •          <Property Name="Addtime" Type="datetime" Nullable="false" />  
  •        </EntityType>  
  •      Csharp中BlogArticleCate实体类的数据

     
    1. <EntityType Name="BlogArticleCate">  
    2.        <Key>  
    3.          <PropertyRef Name="Id" />  
    4.        </Key>  
    5.        <Property Name="Id" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />  
    6.        <Property Name="Author" Type="Int32" Nullable="false" />  
    7.        <Property Name="Name" Type="String" MaxLength="50" FixedLength="false" Unicode="true" Nullable="false" />  
    8.        <Property Name="Remark" Type="String" MaxLength="200" FixedLength="false" Unicode="true" />  
    9.        <Property Name="Statu" Type="Int32" Nullable="false" />  
    10.        <Property Name="IsDel" Type="Boolean" Nullable="false" />  
    11.        <Property Name="Addtime" Type="DateTime" Nullable="false" Precision="3" />  
    12.        <NavigationProperty Name="BlogArticles" Relationship="Self.FK_BlogArticle_BlogArticleCate" FromRole="BlogArticleCate" ToRole="BlogArticle" />  
    13.        <NavigationProperty Name="BlogUser" Relationship="Self.FK_BlogArticleCate_BlogUser" FromRole="BlogArticleCate" ToRole="BlogUser" />  
    14.      </EntityType>  

          数据库中的表与实体之间的对应关系   

     
    1. <EntitySetMapping Name="BlogArticleCates">  
    2.            <EntityTypeMapping TypeName="OumindBlogModel.BlogArticleCate">  
    3.              <MappingFragment StoreEntitySet="BlogArticleCate">  
    4.                <ScalarProperty Name="Id" ColumnName="Id" />  
    5.                <ScalarProperty Name="Author" ColumnName="Author" />  
    6.                <ScalarProperty Name="Name" ColumnName="Name" />  
    7.                <ScalarProperty Name="Remark" ColumnName="Remark" />  
    8.                <ScalarProperty Name="Statu" ColumnName="Statu" />  
    9.                <ScalarProperty Name="IsDel" ColumnName="IsDel" />  
    10.                <ScalarProperty Name="Addtime" ColumnName="Addtime" />  
    11.              </MappingFragment>  
    12.            </EntityTypeMapping>  
    13.          </EntitySetMapping>  

          

     

        看看上面的对应的关系,一个实体类中的属性相对应着数据库表中的一个字段,还有他们分别在C#类中、表中各自的数据类型。正是有了上面的一一对应关系,我才明白:当我们用EF操作实体类中的数据的时候,EF为什么能将实体中“属性”的数据映射到表中的“字段”里面去的。

      

     

    《MVC+EF》——用DBFirst创建ADO.NET实体数据模型和对象关系映射

    标签:点击   正是   text   自己   pat   http   article   文件的   bsp   

    人气教程排行