【问题标题】:Where is the ExecuteStoreCommand method in Entity Framework 5?Entity Framework 5 中的 ExecuteStoreCommand 方法在哪里?
【发布时间】:2012-12-13 09:57:03
【问题描述】:

我在 VS2012 中使用 EF5,我尝试使用 ExecuteStoreCommand 删除某个表的所有数据, 像这样:

ctx.ExecuteStoreCommand("TRUNCATE TABLE [" + tableName + "]");

但问题是 EF 告诉我,找不到方法 ExecuteStoreCommand。我不明白为什么?

你能告诉我为什么吗?或者给我一个高效的解决方案删除表的所有数据。

【问题讨论】:

  • @tomfanning,这取决于他使用的是 ObjectContext 还是 DbContext
  • 正确。 EF5/6 默认是 DbContext (POCO),除非你强制转换它们。

标签: c# entity-framework visual-studio-2012 entity-framework-5


【解决方案1】:

试试这个:

ctx.Database.ExecuteSqlCommand

【讨论】:

  • 这个答案应该被标记为答案...这是一个完美的解决方案。1 投票给你@Justin Harvey
  • 我正在使用:ctx.Database.ExecuteSqlCommand("SET IDENTITY_INSERT profile ON") 但它不起作用...无法插入自定义主键。该怎么做?
猜你喜欢
  • 1970-01-01
  • 2018-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多