“没有这样的列 Extent2。”问题

发布于 2024-10-31 11:45:21 字数 464 浏览 5 评论 0原文

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

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

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

在例外情况下,TargetSite 读取为: {System.Data.Common.DbDataReader ExecuteStoreCommands(System.Data.EntityClient.EntityCommand, System.Data.CommandBehavior)}

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

谢谢

I've inherited a large c# project and am running into issues updating the data model.
I have made my updates in the wysiwyg edmx data modelling editor (vs2010) and the updates seem fine. But it's hard to tell as when I run the program, immediately when it tries to access the database, I get this error:

"SQLite error no such column: Extent2.Country_ID"

Country_ID is a property of an existing entity (which I haven't modified), but I have no idea what "Extent2" is. I did a thorough text search through all related project files and it didn't come up once.

In the exception, the TargetSite reads:
{System.Data.Common.DbDataReader ExecuteStoreCommands(System.Data.EntityClient.EntityCommand, System.Data.CommandBehavior)}

Sadly, there isn't much more info; no error numbers or anything.
Any ideas?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掩于岁月 2024-11-07 11:45:21

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

Extent2 is a table alias in the SQL generated by Entity Framework. It sounds like there's a bad relation or field mapping in your entity model somewhere that's causing the generated SQL commands to not match your actual database structure.

擦肩而过的背影 2024-11-07 11:45:21

如果您的应用程序(在 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;
    }

If your application (which is using the Entity Framework on SQLite) is opening an old version of a database which has the table but not the column you can detect the missing column and add it programmatically as follows:-

    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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文