首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"No such Extent2.<fieldname>“问题

"No such Extent2.<fieldname>“问题
EN

Stack Overflow用户
提问于 2011-04-08 04:21:04
回答 2查看 1.4K关注 0票数 1

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

"SQLite error no such : Extent2.Country_ID“

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

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

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

谢谢

EN

回答 2

Stack Overflow用户

发布于 2011-04-08 04:25:35

Extent2是Entity Framework生成的SQL中的表别名。这听起来像是你的实体模型中某个地方有一个坏的关系或字段映射,导致生成的SQL命令与你的实际数据库结构不匹配。

票数 2
EN

Stack Overflow用户

发布于 2013-10-10 00:26:03

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

代码语言:javascript
复制
    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;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5587126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档