簡體   English   中英

實體框架一對多生成異常

[英]Entity Framework One to Many Generates an Exception

我有2個類的數據庫,並且建立了一對多關系。 這是作為商店設置的(我要針對其運行代碼的主類,該類在我的數據庫中加入了商店出售的產品類別的列表。

因此表是;

shops 
int          id        //shopId & primaryKey
varchar(50) shopName  
...                    //other details left out.

ShopProductTypes
int          id       //Category id
int          ShopId   //Foreign Key to shop table
varchar(50)  CategoryName  
...

這一切都是直截了當的,並且可以通過sql查看器等工作。

我已經導入了數據庫模型,並且取消選中了“ Pluralize / Singularize”框。

我的情況是

 public ReportingContext(string connectionString) : base(connectionString)
        {

            Database.SetInitializer<ReportingContext>(null);

        }

      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
      }

        public DbSet<AutoComp_Reporting.DAL.Shop> Shops{get; set;}

        public DbSet<AutoComp_Reporting.DAL.ShopProductTypes> ShopCategories { get; set; }

    }

但是當我跑步時

var foo = (from s in context.Shops
          where (s.Id == id).select s).toList();

我可以看到所有商店,但是如果嘗試查看QuickWatch中的類別,則會發現以下異常

{“執行命令定義時發生錯誤。有關詳細信息,請參見內部異常。”}

System.Data.EntityException {System.Data.EntityCommandExecutionException}

放松最終揭示出來;

    Message "Invalid column name 'Shops_Id'.\r\nInvalid column name 'Shops_Id'.\r\nInvalid column name 'Shops_Id'."

由於我是實體框架的新手,所以我可能做錯了什么,但是我無法修復此錯誤。 我的數據庫連接字符串中有MultipleActiveResultSets=True

那么,如何找到該錯誤的原因,並/或解決該問題? 我猜想實際的內部異常取決於框架解釋屬性的方式,因此可能是一個紅色鯡魚。

更改表中外鍵的名稱並應用ForeignKey屬性似乎已奏效。 但是,我確定這不是正確的解決方案,否則,它意味着僅當您具有根據特定命名約定構建的數據庫時,該框架才可以使用。

記住這是一個遺留應用程序,如何一勞永逸地解決此問題?

暫無
暫無

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

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