A better way to "Clear" all tables using Entity Framework
Hi currently when I want to clear my tables I use brute force:
inventario_dboEntities inv = new inventario_dboEntities();
foreach (var item in inv.espiromex_dampers)
{
inv.DeleteObje开发者_StackOverflow社区ct(item);
}
foreach (var item in inv.espiromex_detalles)
{
inv.DeleteObject(item);
}
foreach (var item in inv.espiromex_docs)
{
inv.DeleteObject(item);
}
I am sure there must be a better more elegant way to do this... how you guys do this kind of task?
We do it by restoring a "baseline" DB from backup.
Another option you have in Entity Framework is to use ExecuteStorecommand do this db.ExecuteStoreCommand(@"delete table1;delete table2;delete table3;");
I think the best way and best practice is to truncate them in your query browser (since you are using MySQL) or as @TheCloudlessSky said using a stored procedure.
Also (but I am not sure) you could use Reflection and some fancy stuff.
精彩评论