I have tables like
Users (UserId,Username,Password,CreatedOn,CreatedBy)
Roles (RoleId,RoleName,Description,CreatedOn,CreatedBy)
UserRoleMap(UserRoleMapId,UserId,RoleId,CreatedOn,CreatedBy)
These are my entities:
[ActiveRecord(Table="Users")]
public class User:ActiveRecordBase<User>
{
[PrimaryKey(Generator = PrimaryKeyType.Identity, Column = "RoleId")]
public virtual int UserId { get; set; }
[Property(Column="Username")]
public virtual string Username { get; set; }
[Property(Column = "Password")]
public virtual string Password { get; set; }
[Property(Column="CreatedBy")]
public virtual string CreatedBy { get; set; }
[Property(Column="CreatedOn")]
public virtual DateTime CreatedOn { get; set; }
[HasAndBelongsToMany(Table="UserRoleMap",ColumnKey="UserId")]
public IList<Role> Roles { get; set; }
}
[ActiveRecord(Table = "Roles")]
public class Role : ActiveRecordBase<User>
{
[PrimaryKey(Generator = PrimaryKeyType.Identity, Column = "RoleId")]
public virtual int RoleId { get; set; }
[Property(Column = "RoleName")]
public virtual string RoleName { get; set; }
[Property(Column = "Description")]
public virtual string Description { get; set; }
[Property(Column = "CreatedBy")]
public virtual string CreatedBy { get; set; }
[Property(Column = "CreatedOn")]
public virtual DateTime CreatedOn { get; set; }
[HasAndBelongsToMany(Table = "UserRoleMap", ColumnKey = "RoleId")]
public IList<User> Users { get; set; }
}
[ActiveRecord(Table="UserRoleMap")]
public class UserRoleMap:ActiveRecordBase<UserRoleMap>
{
[PrimaryKey(Generator = PrimaryK开发者_JAVA百科eyType.Identity, Column = "UserRoleMapId")]
public virtual int UserRoleMapId { get; set; }
[BelongsTo(Column="UserId",Table="Users")]
public virtual User UserId { get; set; }
[BelongsTo(Column = "RoleId", Table = "Roles")]
public virtual Role RoleId { get; set; }
}
I keep getting this error: ActiveRecordSample.Tests.FrameworkInitializationTest.CanInitializaFramework : Castle.ActiveRecord.Framework.ActiveRecordException : Property UserId references table "Users", which does not have a corresponding [JoinedTable] on the class.
- Wherever the column name matches the property name, you don't need to set Column="..."
- User.UserId is mapped to column "RoleId", it should be "UserId" (or as I said in the above point, just don't define it)
- Make sure to understand the pros and cons of each PK generator.
- When using HasAndBelongsToMany you don't want a separate relationship class (in your case UserRoleMap).
- IIRC you also need to define the other FK in HasAndBelongsToMany with ColumnRef.
精彩评论