Petapoco 在具有一对多关系的同时删除数据?
在我的项目中,我们使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不想使用 db.Query 而想使用 db.Execute :
Instead of using db.Query you would want to use db.Execute :