首页 > 解决方案 > 实体框架错误地跳过了 DB 列

问题描述

解决方案:

项目中有第二个 EDMX 没有生成可视化图表,所以我错误地认为它没有被使用。更新此辅助 EDMX 允许新记录按预期运行。


抱歉,如果我在下面错误地命名任何内容,C# 不是我的强项!

我正在更新一个用 C# .net 构建的旧项目。我之前在这个项目上做了以下没有问题和一些类似的项目,都没有问题。

我正在考虑向数据库添加一个新列,然后更新 EDMX 以考虑新列。在这种情况下,列名是“ExternalURL”。列名在 EDMX 中被正确识别并且文件更新没有错误。类 (category.cs) 也使用新的“ExternalURL”属性成功更新。

最后跳转到代码中,调用 category.ExternalURL 返回一个错误值,但它总是返回 null。我尝试在数据库中手动设置一些值,但它仍然返回 null。尝试在代码中保存一个值并将其保存到数据库会返回成功,并且该属性已附加到该对象,但它从未写入数据库。编辑任何其他属性都可以,例如,category.name 会更新得很好。

我做了一些进一步的挖掘,并找到了一种查看实体框架正在生成的 SQL 语句的方法,它似乎在插入/选择语句中完全丢失了新列。这是一个例子。

SELECT 
    [Extent1].[CategoryID] AS [CategoryID], 
    [Extent1].[Name] AS [Name], 
    [Extent1].[Position] AS [Position], 
    [Extent1].[CreatedOn] AS [CreatedOn], 
    [Extent1].[ModifiedOn] AS [ModifiedOn]
    FROM [dbo].[Category] AS [Extent1]
    ORDER BY [Extent1].[Position] ASC

这里还有一张数据库记录的图片。

数据库选择

此外,这里是如何返回记录的示例。 返回记录

最后是 EDMX。 EDMX

在谷歌搜索的第 3 天,有点迷茫并对所有建议持开放态度。如果需要更多详细信息,请告诉我!

谢谢!

编辑:

处理选择的代码

List<Category> items = Global.DbContext.Categories.OrderBy(x => x.Position).ToList();

        grdData.DataSource = items;
        grdData.DataBind();

处理插入的代码

                Category category = null;

                if (!string.IsNullOrWhiteSpace(txtCategoryID.Value))
                {
                    category = Global.DbContext.Categories.Find(int.Parse(txtCategoryID.Value));
                }


                category.Name = txtName.Text.Trim();
                category.ExternalURL = "Hello";

                Global.DbContext.SaveChanges();

编辑2:

3 次“ExternalURL”在 EDMX 中。

        <EntityType Name="Category">
          <Key>
            <PropertyRef Name="CategoryID" />
          </Key>
          <Property Name="CategoryID" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
          <Property Name="Name" Type="varchar" MaxLength="50" Nullable="false" />
          <Property Name="Position" Type="int" Nullable="false" />
          <Property Name="CreatedOn" Type="datetime" Nullable="false" />
          <Property Name="ModifiedOn" Type="datetime" Nullable="false" />
          <Property Name="ExternalURL" Type="varchar" MaxLength="255" Nullable="false" />
        </EntityType>
        <EntityType Name="Category">
          <Key>
            <PropertyRef Name="CategoryID" />
          </Key>
          <Property Name="CategoryID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
          <Property Name="Name" Type="String" MaxLength="50" Unicode="false" Nullable="false" FixedLength="false" />
          <Property Name="Position" Type="Int32" Nullable="false" />
          <Property Name="CreatedOn" Type="DateTime" Nullable="false" Precision="3" />
          <Property Name="ModifiedOn" Type="DateTime" Nullable="false" Precision="3" />
          <NavigationProperty Name="SubCategories" Relationship="Self.FK_SubCategory_Category" FromRole="Category" ToRole="SubCategory" />
          <Property Name="ExternalURL" Type="String" Nullable="false" MaxLength="255" FixedLength="false" Unicode="false" />
        </EntityType>
<EntitySetMapping Name="Categories">
            <EntityTypeMapping TypeName="IsTypeOf(FileSuppliesModel.Category)">
              <MappingFragment StoreEntitySet="Category">
                <ScalarProperty Name="ExternalURL" ColumnName="ExternalURL" />
                <ScalarProperty Name="ModifiedOn" ColumnName="ModifiedOn" />
                <ScalarProperty Name="CreatedOn" ColumnName="CreatedOn" />
                <ScalarProperty Name="Position" ColumnName="Position" />
                <ScalarProperty Name="Name" ColumnName="Name" />
                <ScalarProperty Name="CategoryID" ColumnName="CategoryID" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>

标签: c#entity-framework

解决方案


我之前遇到过一些这样的场景。我的实体框架数据位于库中(具有特定的连接字符串),而我的测试应用程序有时具有不同的库。你指向同一个数据库吗?我会先检查一下。


推荐阅读