Check if Unique Constraint will be violated before Insert with LLBLGen

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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());
 }
}

If you liked this post, consider subscribing to email updates about my new posts, or sharing it on Hacker News.

Keyboard shortcuts

?
Show this help
Esc
Close this help or an open menu
n
Next (newer) post: Limit Rows In DataTable or DataSet
p
Previous (older) post: PDC 2008 Rollup After 2 Days
Tab
Move between links and buttons (Shift+Tab to go back)