【问题标题】:Linq2db AssociationAttribute implementation returns an errorLinq2db AssociationAttribute 实现返回错误
【发布时间】:2021-03-16 20:41:37
【问题描述】:

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

它返回一个这样的错误。

LinqToDB.Linq.LinqException: 'Expression 'Param_0.CreatedBy' is not a Field.' 但 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

我该如何处理?

最好的问候

【问题讨论】:

    标签: c# asp.net-core associations


    【解决方案1】:

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

    我遇到了同样的异常,并在库存储库中的这个issue 找到了解决方案。

    【讨论】:

      猜你喜欢
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多