Linq2db AssociationAttribute 实现 returns 一个错误
Linq2db AssociationAttribute implementation returns an error
我一直在尝试使用Linq2db LoadWith扩展方法获取相关数据。我的目的是使用 LoadWith 扩展方法获取用户的帖子。
它 returns 是这样的错误。
LinqToDB.Linq.LinqException:“表达式 'Param_0.CreatedBy' 不是字段。” 但 Post 实体 class 有一个 属性 称为 CreatedBy。
这是我的 AppUser class。
public class AppUser : BaseEntity, IAppUser
{
[Required, Identity]
[Key]
public new int Id { get; set; }
[Required]
[ForeignKey("UserDetail")]
public int DetailId { get; set; }
[LinqToDBAssociation.Association(ThisKey = nameof(DetailId), OtherKey = nameof(AppUserDetail.Id), CanBeNull = true, Relationship = Relationship.OneToOne)]
public virtual AppUserDetail UserDetail { get; set; }
public string UserName { get; set; }
public string NormalizedUserName { get; set; }
public string PasswordHash { get; set; }
public bool EmailConfirmed { get; set; }
public string Email { get; set; }
public string NormalizedEmail { get; set; }
public DateTimeOffset? LockoutEnd { get; set; }
public int AccessFailedCount { get; set; }
public bool LockoutEnabled { get; set; }
public string PhoneNumber { get; set; }
public bool PhoneNumberConfirmed { get; set; }
public string SecurityStamp { get; set; }
public bool TwoFactorEnabled { get; set; }
public string ConcurrencyStamp { get; set; }
[LinqToDBAssociation.Association(ThisKey = nameof(Id), OtherKey = nameof(Post.CreatedBy), CanBeNull = true)]
public virtual IEnumerable<Post> UserPosts { get; set; }
}
这是我的 Post class.
public class Post : BaseEntity
{
public Post()
{
PostImages = new HashSet<PostImage>();
PostComments = new HashSet<PostComment>();
PostVideos = new HashSet<PostVideo>();
}
public string Text { get; set; }
public int? PostType { get; set; }
public virtual ICollection<PostComment> PostComments { get; set; }
public virtual ICollection<PostImage> PostImages { get; set; }
public virtual ICollection<PostVideo> PostVideos { get; set; }
}
这是我的 BaseEntity class。
public class BaseEntity : IEntity
{
public int Id { get; set; }
[ForeignKey("CreatedUser")] //By using CreatedUser integration, an owner of post,postComment,postLike that has been created, can be found easily thanks to it.
public virtual int? CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
[ForeignKey("ModifiedUser")] //ModifiedUser can be used in AdminUI.
public int? ModifiedBy { get; set; }
public DateTime? ModifiedDate { get; set; }
public int? StatusId { get; set; } //This can be Enumerations.
public BaseEntity()
{
CreatedDate = DateTime.Now;
}
public virtual AppUser CreatedUser { get; set; }
public virtual AppUser ModifiedUser { get; set; }
}
这个 LoadWith 扩展方法returns那个错误。
var appUser = _appUserRepository.Table.LoadWith(p => p.UserPosts).FirstOrDefault(x => x.UserName == userName);
如果您想查看该项目,可以在此处查看;
https://github.com/dogaanismail/DevPlatform
我该如何处理?
此致
我想您需要将 [LinqToDB.Mapping.Column]
属性添加到您的属性或为整个实体设置 [Table(IsColumnAttributeRequired = false)]
。
我遇到了同样的异常,并在库存储库中的 issue 处找到了解决方案。
我一直在尝试使用Linq2db LoadWith扩展方法获取相关数据。我的目的是使用 LoadWith 扩展方法获取用户的帖子。
它 returns 是这样的错误。
LinqToDB.Linq.LinqException:“表达式 'Param_0.CreatedBy' 不是字段。” 但 Post 实体 class 有一个 属性 称为 CreatedBy。
这是我的 AppUser class。
public class AppUser : BaseEntity, IAppUser
{
[Required, Identity]
[Key]
public new int Id { get; set; }
[Required]
[ForeignKey("UserDetail")]
public int DetailId { get; set; }
[LinqToDBAssociation.Association(ThisKey = nameof(DetailId), OtherKey = nameof(AppUserDetail.Id), CanBeNull = true, Relationship = Relationship.OneToOne)]
public virtual AppUserDetail UserDetail { get; set; }
public string UserName { get; set; }
public string NormalizedUserName { get; set; }
public string PasswordHash { get; set; }
public bool EmailConfirmed { get; set; }
public string Email { get; set; }
public string NormalizedEmail { get; set; }
public DateTimeOffset? LockoutEnd { get; set; }
public int AccessFailedCount { get; set; }
public bool LockoutEnabled { get; set; }
public string PhoneNumber { get; set; }
public bool PhoneNumberConfirmed { get; set; }
public string SecurityStamp { get; set; }
public bool TwoFactorEnabled { get; set; }
public string ConcurrencyStamp { get; set; }
[LinqToDBAssociation.Association(ThisKey = nameof(Id), OtherKey = nameof(Post.CreatedBy), CanBeNull = true)]
public virtual IEnumerable<Post> UserPosts { get; set; }
}
这是我的 Post class.
public class Post : BaseEntity
{
public Post()
{
PostImages = new HashSet<PostImage>();
PostComments = new HashSet<PostComment>();
PostVideos = new HashSet<PostVideo>();
}
public string Text { get; set; }
public int? PostType { get; set; }
public virtual ICollection<PostComment> PostComments { get; set; }
public virtual ICollection<PostImage> PostImages { get; set; }
public virtual ICollection<PostVideo> PostVideos { get; set; }
}
这是我的 BaseEntity class。
public class BaseEntity : IEntity
{
public int Id { get; set; }
[ForeignKey("CreatedUser")] //By using CreatedUser integration, an owner of post,postComment,postLike that has been created, can be found easily thanks to it.
public virtual int? CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
[ForeignKey("ModifiedUser")] //ModifiedUser can be used in AdminUI.
public int? ModifiedBy { get; set; }
public DateTime? ModifiedDate { get; set; }
public int? StatusId { get; set; } //This can be Enumerations.
public BaseEntity()
{
CreatedDate = DateTime.Now;
}
public virtual AppUser CreatedUser { get; set; }
public virtual AppUser ModifiedUser { get; set; }
}
这个 LoadWith 扩展方法returns那个错误。
var appUser = _appUserRepository.Table.LoadWith(p => p.UserPosts).FirstOrDefault(x => x.UserName == userName);
如果您想查看该项目,可以在此处查看;
https://github.com/dogaanismail/DevPlatform
我该如何处理?
此致
我想您需要将 [LinqToDB.Mapping.Column]
属性添加到您的属性或为整个实体设置 [Table(IsColumnAttributeRequired = false)]
。
我遇到了同样的异常,并在库存储库中的 issue 处找到了解决方案。