【问题标题】:"No such column Extent2.<fieldname>" Problems“没有这样的列 Extent2.<fieldname>”问题
【发布时间】:2011-08-01 00:32:30
【问题描述】:

我继承了一个大型 c# 项目,并且在更新数据模型时遇到了问题。 我已经在所见即所得的 edmx 数据建模编辑器(vs2010)中进行了更新,并且更新看起来不错。但是很难说当我运行程序时,当它尝试访问数据库时,我得到了这个错误:

“SQLite 错误没有这样的列:Extent2.Country_ID”

Country_ID 是现有实体的属性(我没有修改),但我不知道“Extent2”是什么。我对所有相关的项目文件进行了彻底的文本搜索,但一次都没找到。

在异常中,TargetSite 显示: {System.Data.Common.DbDataReader ExecuteStoreCommands(System.Data.EntityClient.EntityCommand, System.Data.CommandBehavior)}

遗憾的是,没有更多信息;没有错误号或任何东西。 有什么想法吗?

谢谢

【问题讨论】:

    标签: c# .net data-modeling edmx


    【解决方案1】:

    如果您的应用程序(在 SQLite 上使用实体框架)正在打开旧版本的数据库,其中包含表但没有列,您可以检测缺少的列并以编程方式添加它,如下所示:-

        private EntityFrameworkEntities _modelInstance;
    
        protected override void Load()
        {
            bool retry = false;
            if (!TryLoad(out retry))
            {
                if (retry)
                {
                    AddColumnToTableInDatabase();
                    TryLoad(out retry);
                }
            }
        }
    
        private bool TryLoad(out bool retry)
        {
            bool success = false;
            retry = false;
            using (_modelInstance = new EntityFrameworkEntities())
            {
                _modelInstance.Connection.Open();
    
                var yourQuery = from entity in _modelInstance.Entitys
                                   select entity;
                try
                {
                    foreach (Entity entity in yourQuery)
                    {
                        var vm = new EntityViewModel(entity, this);
                        base.Children.Add(vm);
                    }
                    success = true;
                }
                catch (Exception ex)
                {
                    while (ex.InnerException != null)
                        ex = ex.InnerException;
                    if (ex.Message.ToLower().Contains("no such column") && ex.Message.Split(new char[] { '.' })[1].Equals("Country_ID"))
                        retry = true;
                    log.Error(ex.Message, ex);
                }
            }
            return success;
        }
    
        private bool AddColumnToTableInDatabase()
        {
            bool success = false;
            StringBuilder sql = new StringBuilder(@"ALTER TABLE [Entity] ADD COLUMN [Country_ID] [text] NULL");
            using (_modelInstance = new EntityFrameworkEntities())
            {
                _modelInstance.Connection.Open();
                var connection = (_modelInstance.Connection as EntityConnection).StoreConnection as SQLiteConnection;
                using (var transaction = connection.BeginTransaction())
                {
                    try
                    {
                        using (var command = connection.CreateCommand())
                        {
                            command.CommandText = sql.ToString();
                            command.ExecuteNonQuery();
                        }
                        transaction.Commit();
                        success = true;
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex.Message, ex);
                        transaction.Rollback();
                    }
                }
            }
            return success;
        }
    

    【讨论】:

      【解决方案2】:

      Extent2是Entity Framework生成的SQL中的表别名。听起来您的实体模型中某处存在错误的关系或字段映射,导致生成的 SQL 命令与您的实际数据库结构不匹配。

      【讨论】:

      • 你可能是对的,但遗憾的是,所见即所得的 edmx 编辑器太糟糕了,以至于我最终不用担心框架内的关系,只添加了实体并手动处理了关系
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 2023-01-22
      • 2016-02-11
      相关资源
      最近更新 更多