Tuesday, August 22, 2017

To Add a New Column to an Existing Table in Entity Framework

The "Update Model from Database" is hard/slow to use . It generates other stuff that you probably don't want/need. So manually adding the column that you need will work better. I suggest you do it outside the VS editor since depending on how many models/tables, it can be very slow opening the file in VS.

1. So in Windows Exlorer,right click on the *.edmx file and open with XML (Text) Editor.

 2. Search for the text <EntityType Name="YourTableNameToAddColumn">.

 3. Add the property <Property Name="YourNewColumnName" Type="varchar" MaxLength="50" />

 4. Search for the text <MappingFragment StoreEntitySet="YourTableNameToAddColumn">

 5. Add mapping to the new column <ScalarProperty Name="YourNewColumnName" ColumnName="YourNewColumnName"/>

 6. Save the *.edmx file.

Syntax:

1.Search for the text <EntityType Name="YourTableNameToAddColumn">.

2.Add the property <Property Name="LastName" Type="varchar" MaxLength="50" Nullable="false" />
<EntityType Name="tblUsers">
          <Key>
            <PropertyRef Name="UserID_pk" />
          </Key>
          <Property Name="UserID_pk" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
          <Property Name="FirstName" Type="varchar" MaxLength="50" Nullable="false" />
          <Property Name="MiddleName" Type="varchar" MaxLength="50" />
          <Property Name="LastName" Type="varchar" MaxLength="50" Nullable="false" />        
        </EntityType>

    <EntityType Name="tblUser">
          <Key>
            <PropertyRef Name="UserID_pk" />
          </Key>
          <Property Name="UserID_pk" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
          <Property Name="FirstName" Type="String" Nullable="false" MaxLength="50" FixedLength="false" Unicode="false" />
          <Property Name="MiddleName" Type="String" MaxLength="50" FixedLength="false" Unicode="false" />
          <Property Name="LastName" Type="String" Nullable="false" MaxLength="50" FixedLength="false" Unicode="false" />      
        </EntityType>

3.Search for the text <MappingFragment StoreEntitySet="YourTableNameToAddColumn">
4.Add mapping to the new column <ScalarProperty Name="YourNewColumnName" ColumnName="YourNewColumnName"/>

    <EntitySetMapping Name="tblUsers">
            <EntityTypeMapping TypeName="YourModel.tblUser">
              <MappingFragment StoreEntitySet="tblUsers">              
                <ScalarProperty Name="LastName" ColumnName="LastName" />
                <ScalarProperty Name="MiddleName" ColumnName="MiddleName" />
                <ScalarProperty Name="FirstName" ColumnName="FirstName" />
                <ScalarProperty Name="UserID_pk" ColumnName="UserID_pk" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
 6. Save the *.edmx file

No comments:

Post a Comment

Mixed Content: The page at xxx was loaded over HTTPS, but requested an insecure

 Mixed Content: The page at ' https ://www.test.com/signup.aspx' was loaded over HTTPS, but requested an insecure script ' http ...