C# Linq2db AssociationAttribute实现返回一个错误
我一直在尝试使用Linq2db LoadWith extension方法获取相关数据。我的目的是使用LoadWith扩展方法获取用户的帖子 它返回一个这样的错误 LinqToDB.Linq.LinqException:“表达式'Param_0.CreatedBy'不是字段。”但Post实体类有一个名为CreatedBy的属性 这是我的AppUser类C# Linq2db AssociationAttribute实现返回一个错误,c#,asp.net-core,associations,C#,Asp.net Core,Associations,我一直在尝试使用Linq2db LoadWith extension方法获取相关数据。我的目的是使用LoadWith扩展方法获取用户的帖子 它返回一个这样的错误 LinqToDB.Linq.LinqException:“表达式'Param_0.CreatedBy'不是字段。”但Post实体类有一个名为CreatedBy的属性 这是我的AppUser类 public class AppUser : BaseEntity, IAppUser { [Required, Identity]
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; }
}
此LoadWith扩展方法返回该错误
var appUser = _appUserRepository.Table.LoadWith(p => p.UserPosts).FirstOrDefault(x => x.UserName == userName);
如果你想看这个项目,你可以在这里查看
我怎么办
致以最诚挚的问候我想您需要向属性添加
[LinqToDB.Mapping.Column]
属性,或者为整个实体设置[Table(IsColumnAttributeRequired=false)]
我遇到了相同的异常,并在库存储库中找到了解决方案。我想您需要为整个实体添加
[LinqToDB.Mapping.Column]
属性或设置[Table(IsColumnAttributeRequired=false)]
我遇到了同样的异常,并在库存储库中找到了解决方案
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; }
}
var appUser = _appUserRepository.Table.LoadWith(p => p.UserPosts).FirstOrDefault(x => x.UserName == userName);