开发者

A better way to "Clear" all tables using Entity Framework

开发者 https://www.devze.com 2023-01-08 04:49 出处:网络
Hi currently when I want to clear my tables I use brute force: inventario_dboEntities inv = new inventario_dboEntities();

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号