I created a many to many relationship using ActiveRecord specifically
ActiveRecordValidationBase<Field>. I am creating a form builder tool
for our designers. A form field is related to campaign through the
CampaignFields table which has Id, FieldId, and CampaignId. I created
a UI to assign a field to a campaign. In the controller it's as easy
as instantiating the Campaign, then calling
campaign.Fields.Add(field). However, when I created the action to
remove the field from a campaign, calling
campaign.Fields.Remove(field) failed until I overrode the Equals
method in the Field class. It seems like an ActiveRecordBase or
ActiveRecordValidatorBase<T> class would define an Equals method based
on the Id column or the member marked as PrimaryKey if not
specifically the Id column but the Remove method fails without the
override.
Is it normal to have to override the Equals method to get the Remove
to work in a many to many relationshiip? Below is the method as I
have defined it.
public override bool Equals(object obj)
{
if (this == obj)
{
return true;
}
Field key = obj as Field;
if (key == null)
{
return false;
}
if (Id != key.Id)
{
return false;
}
return true;
}
Here is the definition of the mappings:
Campaign
[HasAndBelongsToMany(typeof(Field), Table="CampaignFields",
ColumnKey="CampaignId", ColumnRef="FieldId")]
public IList<Field> Fields { get; set; }
Field
[HasAndBelongsToMany(typeof(Campaign), Table="CampaignFields",
ColumnKey="FieldId", ColumnRef="CampaignId", Inverse=true)]
public IList<Campaign> Campaigns { get; set; }
CampaignFields
[PrimaryKey]
public int Id { get; private set; }
[BelongsTo("CampaignId")]
public Campaign Campaign { get; set; }
[BelongsTo("FieldId")]
public Field Field { get; set; }
--
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en.