Petapoco 在具有一对多关系的同时删除数据?

发布于 2024-12-09 10:36:04 字数 3824 浏览 0 评论 0原文

在我的项目中,我们使用 petapoco 作为数据访问的 ORM。但是我在删除具有一对多关系的数据时遇到一些困难。我有两个名为用户和组的表。这里,一个用户可以是多个组的成员。因此,用户和组表通过使用另一个名为 UserGroup 的表进行关联,该表在我们的项目中未作为单独的模型公开。那么如何使用 petapoco 从组中删除用户呢?

我的用户模型看起来像这样

[TableName("User")]
[PrimaryKey("UserId")]
[ExplicitColumns]
public class User
{
    [Column("UserId")]
    public int Id { get; set; }

    [Column]
    [Required]
    public Guid Identifier { get; set; }

    [Column("UserStatusId")]
    [Required]
    public UserStatus UserStatus { get; set; }

    [Column("RoleId")]
    [Required]
    public Role Role { get; set; }

    [Column]
    public int? SchoolId { get; set; }

    [Column]
    public int? LanguageId { get; set; }

    [Column]
    public int? LevelId { get; set; }

    [Column("TitleId")]
    public Title Title { get; set; }

    [Column]
    public int? GenderId { get; set; }

    [Column]
    [Required]
    [StringLength(35)]
    public string Username { get; set; }

    [StringLength(100)]
    public string Password { get; set; }

    [Column]
    [StringLength(100)]
    public string PasswordEncrypted { get; set; }

    [Column]
    [StringLength(10)]
    public string PasswordSalt { get; set; }

    [Column]
    //[Required]
    [DataType(DataType.EmailAddress)]
    [StringLength(255)]
    public string Email { get; set; }

    [Column]
    [Required]
    [StringLength(35)]
    public string FirstName { get; set; }

    [Column]
    [Required]
    [StringLength(35)]
    public string LastName { get; set; }

    public string FullName
    { 
        get
        {
            return string.Concat(FirstName, " ", LastName);
        }
    }

    public string FormalName
    {
        get
        {
            if ((int)Title != 0)
            {
                return string.Concat(Title, " ", LastName);
            }
            else
            {
                return LastName;
            }
        }
    }

    [Column]
    public DateTime? DateOfBirth { get; set; }

    [Column]
    public bool Subscribed { get; set; }

    [Column]
    public bool TermsAgreed { get; set; }

    [Column]
    public DateTime? TermsAgreedDate { get; set; }

    [Column]
    public DateTime CreatedDate { get; set; }

    [Column]
    public DateTime ModifiedDate { get; set; }

    [Column]
    public DateTime? LoginDate { get; set; }

    public School School { get; set; }

    public Gender Gender { get; set; }

    public Language Language { get; set; }

    public Level Level { get; set; }

    public IList<Group> Groups { get; set; }
}

我的组模型看起来像这样

[TableName("Group")]
[PrimaryKey("GroupId")]
[ExplicitColumns]
public class Group
{
    [Column("GroupId")]
    public int Id { get; set; }

    [Column]
    public int OwnerUserId { get; set; }

    [Column]
    public string Name { get; set; }

    [Column]
    public DateTime CreatedDate { get; set; }

    [Column]
    public DateTime ModifiedDate { get; set; }

    public User OwnerUser { get; set; }

    public IList<User> Users { get; set; }
}

我已经完成了这样的删除

    /// <summary>
    /// Delete users from groups
    /// </summary>
    /// <param name="userIds">user Id list</param>
    /// <param name="groupId">group Id</param>
    public void DeleteUsersFromGroup(List<int> userIds, int groupId)
    {
        using (Database db = this.GetDatabase())
        {
            var sql = Sql.Builder.Append(
                @"DELETE [UserGroup]
                LEFT OUTER JOIN [User] ON [UserGroup].[UserId] = [User].[UserId]
                WHERE [UserGroup].[UserId] IN (@0) AND [UserGroup].[GroupId] = @1",
                userIds,
                groupId);
            db.Query<User>(sql);
        }
    }

有人可以帮助我解决这个问题吗?

In my project we are using petapoco as our ORM for data access. But i face some difficulties while deleting datas which have one to many relations. I have two tables named user and group. Here one user can be a memberof more than one group. so the user and group table is related by using another table named UserGroup which is not exposed in our project as a seperate model. So how can i delete the users from a group using petapoco?

My user model looks like this

[TableName("User")]
[PrimaryKey("UserId")]
[ExplicitColumns]
public class User
{
    [Column("UserId")]
    public int Id { get; set; }

    [Column]
    [Required]
    public Guid Identifier { get; set; }

    [Column("UserStatusId")]
    [Required]
    public UserStatus UserStatus { get; set; }

    [Column("RoleId")]
    [Required]
    public Role Role { get; set; }

    [Column]
    public int? SchoolId { get; set; }

    [Column]
    public int? LanguageId { get; set; }

    [Column]
    public int? LevelId { get; set; }

    [Column("TitleId")]
    public Title Title { get; set; }

    [Column]
    public int? GenderId { get; set; }

    [Column]
    [Required]
    [StringLength(35)]
    public string Username { get; set; }

    [StringLength(100)]
    public string Password { get; set; }

    [Column]
    [StringLength(100)]
    public string PasswordEncrypted { get; set; }

    [Column]
    [StringLength(10)]
    public string PasswordSalt { get; set; }

    [Column]
    //[Required]
    [DataType(DataType.EmailAddress)]
    [StringLength(255)]
    public string Email { get; set; }

    [Column]
    [Required]
    [StringLength(35)]
    public string FirstName { get; set; }

    [Column]
    [Required]
    [StringLength(35)]
    public string LastName { get; set; }

    public string FullName
    { 
        get
        {
            return string.Concat(FirstName, " ", LastName);
        }
    }

    public string FormalName
    {
        get
        {
            if ((int)Title != 0)
            {
                return string.Concat(Title, " ", LastName);
            }
            else
            {
                return LastName;
            }
        }
    }

    [Column]
    public DateTime? DateOfBirth { get; set; }

    [Column]
    public bool Subscribed { get; set; }

    [Column]
    public bool TermsAgreed { get; set; }

    [Column]
    public DateTime? TermsAgreedDate { get; set; }

    [Column]
    public DateTime CreatedDate { get; set; }

    [Column]
    public DateTime ModifiedDate { get; set; }

    [Column]
    public DateTime? LoginDate { get; set; }

    public School School { get; set; }

    public Gender Gender { get; set; }

    public Language Language { get; set; }

    public Level Level { get; set; }

    public IList<Group> Groups { get; set; }
}

My group model looks like this

[TableName("Group")]
[PrimaryKey("GroupId")]
[ExplicitColumns]
public class Group
{
    [Column("GroupId")]
    public int Id { get; set; }

    [Column]
    public int OwnerUserId { get; set; }

    [Column]
    public string Name { get; set; }

    [Column]
    public DateTime CreatedDate { get; set; }

    [Column]
    public DateTime ModifiedDate { get; set; }

    public User OwnerUser { get; set; }

    public IList<User> Users { get; set; }
}

Any i have done deletion like this

    /// <summary>
    /// Delete users from groups
    /// </summary>
    /// <param name="userIds">user Id list</param>
    /// <param name="groupId">group Id</param>
    public void DeleteUsersFromGroup(List<int> userIds, int groupId)
    {
        using (Database db = this.GetDatabase())
        {
            var sql = Sql.Builder.Append(
                @"DELETE [UserGroup]
                LEFT OUTER JOIN [User] ON [UserGroup].[UserId] = [User].[UserId]
                WHERE [UserGroup].[UserId] IN (@0) AND [UserGroup].[GroupId] = @1",
                userIds,
                groupId);
            db.Query<User>(sql);
        }
    }

Do anyone give me a help in resolving this?

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

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

发布评论

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

评论(1

静待花开 2024-12-16 10:36:04

您不想使用 db.Query 而想使用 db.Execute :

db.Execute(@"DELETE [UserGroup]
            LEFT OUTER JOIN [User] ON [UserGroup].[UserId] = [User].[UserId]
            WHERE [UserGroup].[UserId] IN (@0) AND [UserGroup].[GroupId] = @1",
            userIds,
            groupId);

Instead of using db.Query you would want to use db.Execute :

db.Execute(@"DELETE [UserGroup]
            LEFT OUTER JOIN [User] ON [UserGroup].[UserId] = [User].[UserId]
            WHERE [UserGroup].[UserId] IN (@0) AND [UserGroup].[GroupId] = @1",
            userIds,
            groupId);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文