簡體   English   中英

實體框架錯誤地跳過了 DB 列

[英]Entity Framework incorrectly skips DB column

解決方案:

項目中有第二個 EDMX 沒有生成可視化圖表,所以我錯誤地認為它沒有被使用。 更新此輔助 EDMX 允許新記錄按預期運行。


抱歉,如果我在下面錯誤地命名任何內容,C# 不是我的強項!

我正在更新在 C# .net 中構建的舊項目。 我之前在這個項目上做了以下沒有問題和一些類似的項目,都沒有問題。

我正在考慮向數據庫添加一個新列,然后更新 EDMX 以考慮新列。 在這種情況下,列名是“ExternalURL”。 列名在 EDMX 中被正確識別並且文件更新沒有錯誤。 class (category.cs) 也使用新的“ExternalURL”屬性成功更新。

最后跳轉到代碼中,調用 category.ExternalURL 返回一個錯誤的值,但它總是返回 null。 我嘗試在數據庫中手動設置一些值,但它仍然返回 null。 嘗試在代碼中保存一個值並將其保存到數據庫會返回成功,並且該屬性已附加到 object,但它從未寫入數據庫。 編輯任何其他屬性都可以,例如,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

這里還有一張數據庫記錄的圖片。

數據庫 Select

此外,這里是如何返回記錄的示例。 返回記錄

最后是 EDMX。 EDMX

在谷歌搜索的第 3 天,有點迷茫並對所有建議持開放態度。 如果需要更多詳細信息,請告訴我!

謝謝!

編輯:

處理 select 的代碼

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>

我之前遇到過一些這樣的場景。 我的實體框架數據位於庫中(具有特定的連接字符串),而我的測試應用程序有時具有不同的庫。 你指向同一個數據庫嗎? 我會先檢查一下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM