我有一个迁移到 Entity Framework Core 的应用程序。但我有一些应用程序需要支持此迁移。他们的表中有一个不再存在的列(它位于另一个地方)。
我正在尝试弄清楚如何使用 Entity Framework Core 应用程序读取旧数据库并提取列信息(如果存在)。
非常感谢任何对此的帮助。
编辑:为了缩小范围。
我在旧的 sqlite 数据库中有一个表,其中有一列。我想使用 Entity Framework Core 从旧数据库中提取该列,以便将其插入新位置。
最佳答案
您可以编写自己的 SQL 代码,而不是使用 Entity Framework 抽象。
我个人使用我在网上找到的东西(不再找到原始来源)来添加链接到我的自定义查询的通用类支持:
public class MyRefDatabaseContext: RefDatabaseContext
{
public List<T> ExecSQL<T>(string query)
{
using (var command = Database.GetDbConnection().CreateCommand())
{
command.CommandText = query;
command.CommandType = CommandType.Text;
Database.OpenConnection();
List<T> list = new List<T>();
using (var result = command.ExecuteReader())
{
T obj = default(T);
while (result.Read())
{
obj = Activator.CreateInstance<T>();
foreach (PropertyInfo prop in obj.GetType().GetProperties())
{
if (!object.Equals(result[prop.Name], DBNull.Value))
{
prop.SetValue(obj, result[prop.Name], null);
}
}
list.Add(obj);
}
}
Database.CloseConnection();
return list;
}
}
}
编辑:为了清楚起见,我添加了包装类
关于c# - 在 Entity Framework Core 中获取没有模型的表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49563076/