Active Record 2.0, need help using CompositeKeys.

1 view
Skip to first unread message

Geir Inge Imsland

unread,
Nov 23, 2009, 5:30:24 AM11/23/09
to Castle Project Users
Hi there.
Consider the following database:
Table User:
Id, PrimaryKey
Name

Table Group:
Id, PrimaryKey
Name

Table UserGroup:
UserId, PK, FK
GroupId PK, FK
Active


In the class User.cs, I have a method that gets all of the UserGroups
objects that the User has. I need to have them, because the
relationship between User and Group has a property (Active). Here is
one variant that I have tried:
[HasAndBelongsToMany(typeof(UserGroup),
Table="UserGroup",
ColumnRef="userId",
CompositeKeyColumnKeys=new string[] { "UserId",
"GroupId" },
Inverse=true)]
public IList<UserGroup> UserGroups
{
get { return _userGroups; }
set { _userGroups = value; }
}

The above code will give me this error:
Foreign key (FKB485EDD13D0DB589:UserGroup [UserId])) must have same
number of columns as the referenced primary key (UserGroup [UserId,
GroupId])

How should I do this?

The code for UserGroup.cs with ActiveRecord attributes follows:
[Serializable]
public class UserGroupKey
{
private int groupId;
private int userId;

[KeyProperty(Column="UserId")]
public int UserId
{
get { return userId; }
set { userId = value; }
}

[KeyProperty(Column="GroupId")]
public int GroupId
{
get { return groupId; }
set { groupId = value; }
}

public override int GetHashCode()
{
return userId ^ groupId;
}

public override bool Equals(object obj)
{
if (this == obj)
{
return true;
}
UserGroupKey key = obj as UserGroupKey;
if (key == null)
{
return false;
}
if (userId != key.userId || groupId != key.groupId)
{
return false;
}
return true;
}
}

[ActiveRecord("UserGroup")]
public class UserGroup : ActiveRecordBase<UserGroup>
{
private UserGroupKey _id;
private int _active;

[CompositeKey]
public UserGroupKey Id
{
get { return _id; }
set { _id = value; }
}

[Property("Active")]
public decimal Active
{
get { return _active; }
set { _active = value; }
}

public UserGroup()
{
}
}
Reply all
Reply to author
Forward
0 new messages