没有这样的列Extent2."问题
本文关键字:fieldname quot 问题 Extent2 | 更新日期: 2023-09-27 17:49:38
我继承了一个大型c#项目,在更新数据模型时遇到了问题。我已经在wysiwyg edmx数据建模编辑器(vs2010)中进行了更新,更新看起来很好。但是很难判断,因为当我运行程序时,当它试图访问数据库时,我立即得到这个错误:
"SQLite error no such column: Extent2. "Country_ID "
Country_ID是一个现有实体的属性(我没有修改),但我不知道"Extent2"是什么。我在所有相关的项目文件中进行了彻底的文本搜索,但一次也没有出现。
在例外情况下,TargetSite为:{System.Data.Common。DbDataReader ExecuteStoreCommands (System.Data.EntityClient。EntityCommand System.Data.CommandBehavior)}
遗憾的是,没有更多的信息;没有错误编号之类的。什么好主意吗?
谢谢
Extent2
是实体框架生成的SQL中的表别名。这听起来像是有一个坏的关系或字段映射在您的实体模型的某个地方,导致生成的SQL命令不匹配您的实际数据库结构。
如果你的应用程序(在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;
}