繁体   English   中英

Linq2db AssociationAttribute 实现返回错误

[英]Linq2db AssociationAttribute implementation returns an error

我一直在尝试通过使用 Linq2db LoadWith 扩展方法来获取相关数据。 我的目的是通过使用 LoadWith 扩展方法获取用户的帖子。

它返回一个这样的错误。

LinqToDB.Linq.LinqException: '表达式'Param_0.CreatedBy' 不是一个字段。 但 Post 实体类有一个名为 CreatedBy 的属性。

在此处输入图片说明

这是我的 AppUser 类。

 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 课程。

 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 类。

    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 扩展方法返回该错误。

 var appUser = _appUserRepository.Table.LoadWith(p => p.UserPosts).FirstOrDefault(x => x.UserName == userName);

如果你想看这个项目,你可以在这里查看;

https://github.com/dogaanismail/DevPlatform

我该如何处理?

此致

我想您需要将[LinqToDB.Mapping.Column]属性添加到您的属性或为整个实体设置[Table(IsColumnAttributeRequired = false)]

我遇到了同样的异常,并在库存储库中找到了解决此问题的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM