edmx 文件编辑

文件是一个xml文件,主要关注三部分:

  • 数据库实体:edmx:StorageModels
  • 类实体:edmx:ConceptualModels
  • 数据库跟类实体的映射关系:edmx:Mappings


操作:手动添加一个字段

下面举例给文章添加一个摘要字段Summary

1、给文章类添加相关属性

public string Summary { get; set; }

2、edmx文件中定位到 edmx:StorageModels,找到数据库实体定义节点(例如 Name="表名"),在节点内添加Property节点,如下所示

<Property Name="Summary" Type="varchar" Nullable="false" />

3、edmx文件中定位到 edmx:ConceptualModels,找到类实体定义节点(例如 Name="类名"),在节点内添加Property节点,如下所示

<Property Name="Summary" Type="String" Nullable="false" />

4、edmx文件中定位到 edmx:Mappings,找到映射定义节点(例如 Name="表名"),在节点内添加ScalarProperty节点,如下所示

<ScalarProperty Name="Summary" ColumnName="Summary" />

5、保存完成



附录1:文件示例代码

<?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="edmxModel.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="User">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="int" Nullable="false" />
          <Property Name="Name" Type="nvarchar" MaxLength="500" />
          <Property Name="Type" Type="int" Nullable="false" />
        </EntityType>
        <EntityContainer Name="edmxModelStoreContainer">
          <EntitySet Name="User" EntityType="Self.User" Schema="dbo" store:Type="Tables" />
        </EntityContainer>
      </Schema>
  </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema Namespace="edmxModel" 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="User">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="Int32" Nullable="false" />
          <Property Name="Name" Type="String" MaxLength="500" FixedLength="false" Unicode="true" />
          <Property Name="Type" Type="Int32" Nullable="false" />
        </EntityType>
        <EntityContainer Name="edmxEntities" annotation:LazyLoadingEnabled="true">
          <EntitySet Name="User" EntityType="Self.User" />
        </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="edmxModelStoreContainer" CdmEntityContainer="edmxEntities">
          <EntitySetMapping Name="User">
            <EntityTypeMapping TypeName="edmxModel.User">
              <MappingFragment StoreEntitySet="User">
                <ScalarProperty Name="Type" ColumnName="Type" />
                <ScalarProperty Name="Name" ColumnName="Name" />
                <ScalarProperty Name="Id" ColumnName="Id" />
              </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="false" />
        <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>