帮助在关联表的情况下在 Castle Active Record 中配置关联

发布于 2024-11-07 17:55:58 字数 2215 浏览 6 评论 0原文

我有像

Users (UserId,Username,Password,CreatedOn,CreatedBy)

Roles (RoleId,RoleName,Description,CreatedOn,CreatedBy)

UserRoleMap(UserRoleMapId,UserId,RoleId,CreatedOn,CreatedBy)

这样的表,这些是我的实体:

[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 = PrimaryKeyType.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; }

}

我不断收到此错误: ActiveRecordSample.Tests.FrameworkInitializationTest.CanInitializaFramework : Castle.ActiveRecord.Framework.ActiveRecordException : 属性 UserId 引用表“Users”,该表在类上没有相应的 [JoinedTable]。

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 = PrimaryKeyType.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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

姐不稀罕 2024-11-14 17:55:58
  • 只要列名与属性名称匹配,您就不需要设置 Column="..."
  • User.UserId 映射到列“RoleId”,它应该是“UserId”(或者正如我在上面所说的那样,只是不要定义它)
  • 确保理解 每个 PK 生成器的优缺点
  • 当使用 HasAndBelongsToMany 时,您不需要单独的关系类(在您的情况下为 UserRoleMap)。
  • IIRC 您还需要使用 ColumnRef。
  • 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文