Here is my snippet code how to create a workflow association that can then be added to a list.
/// <summary>
/// Add workflow association to specific list
/// </summary>
/// <param name="siteId">Site collection object</param>
/// <param name="webId">SharePoint website object</param>
/// <param name="strWorkflowAssociateToListTitle">List title of SPList object will be associated workflow</param>
/// <param name="workflowName">The name of the desired workflow association.</param>
/// <param name="workflowId">Workflow template based on template ID</param>
/// <param name="workflowHistoryListTitle">List title of Workflow History list</param>
/// <param name="taskListTitle">List title of Task List</param>
/// <param name="autoStartWorkflowCreate">A Boolean that represents whether the workflow starts
/// automatically when a new item is created.</param>
public static void AssociateWorklowToList(Guid siteId, Guid webId, string strWorkflowAssociateToListTitle, string workflowName,
Guid workflowId, string workflowHistoryListTitle, string taskListTitle, bool autoStartWorkflowCreate)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite siteCollection = new SPSite(siteId))
{
siteCollection.AllowUnsafeUpdates = true;
using (SPWeb site = siteCollection.OpenWeb(webId))
{
bool allowUnsafeUpdates = false;
allowUnsafeUpdates = site.AllowUnsafeUpdates;
site.AllowUnsafeUpdates = true;
SPListCollection lists = site.Lists;
////a workflow association that can then be added to a this list.
SPList workflowAssociateToList = lists[strWorkflowAssociateToListTitle];
////workflow history list
SPList workflowHistoryList = lists[workflowHistoryListTitle];
////task list
SPList taskList = lists[taskListTitle];
SPWorkflowAssociation
existingAssociation = workflowAssociateToList.WorkflowAssociations
.GetAssociationByName(workflowName,
System.Globalization.CultureInfo.CurrentCulture);
if (existingAssociation == null)
{
SPWorkflowManager workflowManager = siteCollection.WorkflowManager;
SPWorkflowTemplateCollection templates = workflowManager.GetWorkflowTemplatesByCategory(site, null);
SPWorkflowTemplate template = templates.GetTemplateByBaseID(workflowId);
SPWorkflowAssociation association = SPWorkflowAssociation.CreateListAssociation(template, workflowName, taskList, workflowHistoryList);
////the key solution
site.Update();
association.AllowManual = true;
association.AutoStartCreate = false;
workflowAssociateToList.AddWorkflowAssociation(association);
workflowAssociateToList.Update();
association.Enabled = true;
}
site.AllowUnsafeUpdates = allowUnsafeUpdates;
}
}
});
}