我正在维护这个应用程序(A),它正在写入另一个应用程序(B)的表。问题是 A 写入了许多 B,并且模型在 B 的新版本中发生了变化。
示例:A 具有实体 Dog,其列为:姓名、年龄、性别
在 B 的大多数情况下,该实体与表匹配。但最新版本的 B Dog 有以下列:Name、Age、Sex、FavoritFood(不允许为空)
我无法更改 B 的数据库方案,无论是通过代码还是 SQL Server。如果我这样做,B 就会根据需要重新设计它。我可以更改 A 的 Dog 实体,但这需要区分 B 的新版本和旧版本。
A 使用 Entity Framework 6.2 作为 ORM。
到目前为止我的想法如下:检查列是否存在,如果不存在则忽略该字段。
protected override void OnModelCreating(DbModelBuilder builder) {
base.OnModelCreating(builder);
if (!Database.CompatibleWithModel(true)) {
builder.Entity<Dog>().Ignore(_ => _.FavoritFood);
}
}
Run Code Online (Sandbox Code Playgroud)
我不仅无法从 OnModelCreating 中访问上下文,而且我还发现缺乏这种可能性,因为它非常通用,我想专门检查 FavoritFood 列。
我怎样才能做到这一点?
我现在已经连续工作了2个小时并阅读了很多这样的问题,但是我无法看到我的OleDbCommand在哪里或者为什么没有按照应有的方式工作.
我把这段代码写成了我见过的所有问题和答案的合并:
using (var connection = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;" + @"data source= *path to mdp*"))
{
try
{
connection.Open();
foreach (KeyValuePair<string, string> pair in dictionary)
{
string query = "SELECT * FROM mytable WHERE db_id=?";
var command = new OleDbCommand(query, connection);
//command.Parameters.Add(new OleDbParameter("@ID", pair.Value));
command.Parameters.Add("?", OleDbType.BSTR).Value = pair.Value;
var reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader[0].ToString());
}
reader.Close();
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Run Code Online (Sandbox Code Playgroud)
然而,这给了我"给出一个或多个必需的Parameteres没有值"错误.如果我尝试现在评论的行
command.Parameters.Add(new OleDbParameter("@ID", pair.Value));
Run Code Online (Sandbox Code Playgroud)
并评论另一方,结果完全相同.
但是,如果我同时使用这两行,那么阅读器会读取我的表格列中的每个条目,而不仅仅是与pair.Value的所需匹配.
KeyValuePair只是一个字符串id的元组,它来自我的程序作为一个键,它在数据库中的相应id作为值.
我很感谢任何帮助或建议.
我将此 BrowserRouter 组件作为我的顶级组件,其状态中有一个标题字符串。该组件呈现一个 NavigationComponent,它基本上是一个导航栏,我想在其中设置一个标题,该标题会根据我在网站上的位置而变化。
该组件大致如下所示
changeTitle(title) {
this.setState({
title: title,
})
}
<BrowserRouter>
<div>
<NavigationComponent title={this.state.title}/>
<Switch>
<Route exact path="/" render={(props) => (
<App {...props} changeTitle= {this.changeTitle.bind(this)}/>
)} />
<Route exact path="/login" component={LoginComponent }/>
<Route exact path="/logout" component={LoginComponent }/>
</Switch>
</div>
</BrowserRouter>
Run Code Online (Sandbox Code Playgroud)
现在我知道如何将该方法公开changeTitle给应用程序组件。但我想进一步公开该方法,我在 atm 上实现它的方式是这样的:
class App extends React.Component{
constructor(props){
super(props);
}
changeTitle(title) {
this.props.changeTitle(title);
}
render(){
return(
<OverviewComponent changeTitle= {this.changeTitle.bind(this)}/>
}
} // note: App component stripped down
Run Code Online (Sandbox Code Playgroud)
在概述组件中,我第一次实际调用changeTitle函数。
我现在的问题是:是否有更聪明的方法来实现这一点,而无需将方法changeTitle通过管道传递给每个孩子?在 OverviewComponent 之后还有更多的组件被调用,这些组件也需要更改标题。