1

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

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

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

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

可悲的是,没有更多信息。没有错误号或任何东西。有任何想法吗?

谢谢

4

2 回答 2

2

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

于 2011-04-07T20:25:34.827 回答
0

如果您的应用程序(在 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;
    }
于 2013-10-09T16:26:02.603 回答