Thursday, April 21, 2011

How to delete all records in a table using SubSonic 3

I am a little stuck at the moment trying to delete all the records from a table using this approach:

new Delete<Contact>().Execute();

This statement fails with a NullReferenceException in BuildDeleteStatement method at line:

sb.Append(query.FromTables[0].QualifiedName);

because, although FromTables has one entry, it is set to null. I also tried this but it didn't worked either:

var provider = ProviderFactory.GetProvider("MonitorData");
new Delete<Contact>(provider).Execute();

What am I doing wrong? Please help. Thanks.

PS: I am using ActiveRecord with SS v3.0.0.3.

From stackoverflow
  • You can do this with the repo DeleteMany method:

    SubSonicRepository<Contact> repo = new SubSonicRepository<Contact>(new YourDB());
    repo.DeleteMany(contact => true);
    

    The lambda I'm using is just to ensure all records are selected (thanks dangph).

    Yogesh : Thanks. It did the trick.
    dangph : repo.DeleteMany(c => true);
    Yogesh : You mean "repo.DeleteMany(c => true);" ?

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.