Skip to content

Instantly share code, notes, and snippets.

@mps
Created March 24, 2011 12:45
Show Gist options
  • Save mps/884992 to your computer and use it in GitHub Desktop.
Save mps/884992 to your computer and use it in GitHub Desktop.
Delete everything for an Agency
private static void Delete(string customerCode)
{
MongoInitializer.Initialize();
var connectionString = ConfigurationManager.ConnectionStrings["AccuAutoMongo"];
MServer = MongoServer.Create(connectionString.ConnectionString);
MDatabase = MServer[ConfigurationManager.AppSettings["DbName"] ?? "AccuAuto"];
var clientCollection = MDatabase.GetCollection<Mongo.Data.Client>("Client");
var paymentCollection = MDatabase.GetCollection<Mongo.Data.Payment>("Payment");
var attachmentCollection = MDatabase.GetCollection<Mongo.Data.FileAttachment>("FileAttachment");
var commissionCollection = MDatabase.GetCollection<Mongo.Data.ManagementCommission>("ManagementCommission");
var policyCollection = MDatabase.GetCollection<Policy>("Policy");
var factory = new MongoConversionRepositories.ConversionRepositoryFactory();
var agencyRepo = factory.BuildAgencyRepository();
var agency = agencyRepo.GetByCustomerCode(customerCode);
if (agency == null)
throw new ArgumentException("Could not find the agency for customercode: {0}", customerCode);
var paymentQuery = Query<Mongo.Data.Payment>.EQ(p => p.OldAgencyId, agency.Id);
paymentCollection.Remove(paymentQuery);
var attachmentQuery = Query<Mongo.Data.FileAttachment>.EQ(p => p.OldAgencyId, agency.Id);
attachmentCollection.Remove(attachmentQuery);
var clientQuery = Query<Mongo.Data.Client>.EQ(p => p.OldAgencyId, agency.Id);
clientCollection.Remove(clientQuery);
var policyQuery = Query<Policy>.EQ(p => p.AgencyId, agency.Id);
policyCollection.Remove(policyQuery);
var commissionQuery = Query<Mongo.Data.ManagementCommission>.EQ(p => p.OldAgencyId, agency.Id);
commissionCollection.Remove(commissionQuery);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment