Check if Unique Constraint will be violated before Insert with LLBLGen
Date Published: 30 October 2008
I needed to determine if a unique constraint would be violated so that I could programmatically update the Name of a business object to make it unique today. I use LLBLGen for this projectβs database layer, and I have a unique constraint (actually a unique index) set on the database. In this case it covers two columns, an integer ID and a string Name, but the nice thing about LLBLGen is that it provides helper methods that generate the predicate needed. The code required looked like this:
public static bool CampaignNameInUse(int advertiserId, string campaignName)
{
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
CampaignEntity myCampaign = new CampaignEntity();
myCampaign.AdvertiserId = advertiserId;
myCampaign.Name = campaignName;
return adapter.FetchEntityUsingUniqueConstraint(myCampaign, myCampaign.ConstructFilterForUCAdvertiserIdName());
}
}
Tags - Browse all tags
Category - Browse all categories
About Ardalis
Software Architect
Steve is an experienced software architect and trainer, focusing on code quality and Domain-Driven Design with .NET.