2

我有一个包含 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'."

I may well be doing something wrong as I am new to entity framework, but I cannot fix this error. I have got MultipleActiveResultSets=True in my database connection string.

So how do I track down the cause of this error, and / or fix it? I am guessing that the actual inner exception is down to the way that the framework is interpreting the properties, so may be a red herring..

4

1 回答 1

0

Changing the name of the Foreign Key in the table, and applying the ForeignKey attribute seems to have worked. However I am sure that this is not the correct solution, else it means that the framework only works out of the box if you have a database that has been built to a specific naming convention.

Bearing in mind this is a legacy application, how do I fix this issue once and for all?

于 2013-02-18T14:16:37.340 回答