当我尝试像这样嵌套的预加载时
$journal = Journal::with(['pages','pages.articles'])->select('id','titre','date')->findOrFail($id);
Run Code Online (Sandbox Code Playgroud)
这是有效的,但是当我尝试为嵌套关系添加选择以仅获取如下所示的特定列时:
$journal =Journal::with(['pages'=> function ($query) {
$query->select('id','thumbnail');
},'pages.articles'=> function ($query) {
$query->select('id','images','areas');
}])->findOrFail($id);
Run Code Online (Sandbox Code Playgroud)
我只得到Journal空的父模型()pages
{
"id": 3,
"numero": "2219",
"titre": "J2219",
"date": "2018-12-13",
"created_at": "2019-01-03 10:16:59",
"updated_at": "2019-01-08 11:04:42",
"deleted_at": null,
"pages": []
}
Run Code Online (Sandbox Code Playgroud) 在我的 Rails v5.2.4.1 应用程序 (ruby v2.6.1) 中,我使用工作查询加载一堆消息。每条消息都有belongs_to一个:usermodel 和 user has_one_attached :photo。我渴望向用户加载所有这样的消息:
Message.<query>.includes(:user)
Run Code Online (Sandbox Code Playgroud)
现在,这段代码photo.attached? ? photo : 'default_avatar.png'会产生如下查询:
SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 32], ["record_type", "User"], ["name", "photo"], ["LIMIT", 1]]
Run Code Online (Sandbox Code Playgroud)
我如何立即加载user.photo我user.photo.attached?的查询?
我的用户表中有大量行,我需要应用急切加载来加载用户的评论。
User.includes(:comments)
由于用户集太大,因此在添加急切加载时会消耗大量内存。
因此,在经历了几个解决方案之后,我以下面的方式结束
User.select(:id).find_in_batches do |user|
users = User.where(id: user_id).includes(:comments)
end
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来使用 find_in_batches 执行急切加载?
这是我的 SQL 查询,我在 linqpad 中测试了它,它有效,但它在 EF Core 3.1 中不起作用:
from v in JournalVoucherLines
group v by v.AccountId into vg
join bp in Parties on vg.FirstOrDefault().AccountId equals bp.AccountId
select new
{
name = bp.FullName,
key = vg.Key,
Creditor = vg.Sum(v => v.Credit),
Deptor = vg.Sum(v => v.Debit),
RemainAmount = vg.Sum(v => v.Credit) - vg.Sum(v => v.Debit)
}
Run Code Online (Sandbox Code Playgroud)
当我在 EF Core 中使用查询时,出现以下异常:
无法翻译LINQ 表达式“(GroupByShaperExpression: KeySelector: (j.AccountId), ElementSelector:(EntityShaperExpression: EntityType: JournalVoucherLine ValueBufferExpression: (ProjectionBindingExpression: EmptyProjectionMember) IsNullable: False ) ).FirstOrDefault()”。以可翻译的形式重写查询,或者通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用来显式切换到客户端计算。有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038 。 …
我一直在使用 TypeORM,在推送数据时遇到一些问题,如下所示:
User.ts(实体)
@OneToMany(type => Post, post => post.user)
posts: Post[]
Run Code Online (Sandbox Code Playgroud)
Post.ts(实体)
@ManyToOne(type => User, user => user.posts)
user: User
Run Code Online (Sandbox Code Playgroud)
当我尝试像这样创建帖子时:
createPost.ts
// Some Codes...
// Save Post
const post = Post.create({
title: title,
content: content
})
await post.save()
// Update User
const user = await User.findOne({ uid: ctx.uid })
user.posts.push(post) // ****************** PROBLEM ******************
await user.save()
Run Code Online (Sandbox Code Playgroud)
正如我在上面标记的问题,当我请求这个时,我得到了Cannot call method 'push of undefined错误。
但幸运的是,我可以通过在User.ts文件中添加以下选项来解决这个问题:
@OneToMany(type => Post, post => post.user, { …Run Code Online (Sandbox Code Playgroud) 我正在使用Nhibernate作为我的ORM.
我有一个"Control"类,它与ControlDetail有一对多的关系(即一个控件有很多controlDetails).
在控件xml配置中,它具有以下内容
<bag name="ControlDetails" lazy="true" access="property" order-by="SortOrder asc" cascade="all-delete-orphan"
table="ControlDetail">
<key column="ControlID"/>
<one-to-many class="ControlDetail"/>
</bag>
Run Code Online (Sandbox Code Playgroud)
这样我相信除非另有说明,它会延迟加载控件的控件.
我正在运行NHProf来尝试解决我们遇到的一些性能问题,它已经在这些类中找到了一个Select N + 1问题.
我们正在使用存储库DA层,我试图看看我是否可以在需要时添加以急切获取数据的方式并提出这个.
public T GetById<T>(Int32 id, List<string> fetch) where T : BaseObject
{
T retObj = null;
ISession session = EnsureCurrentSession();
{
ICriteria criteria = session.CreateCriteria(typeof (T));
criteria.SetCacheable(true);
criteria.Add(Expression.Eq("Id", id));
foreach(var toFetch in fetch)
{
criteria.SetFetchMode(toFetch, FetchMode.Eager);
}
retObj = criteria.List<T>().FirstOrDefault();
}
return retObj;
}
Run Code Online (Sandbox Code Playgroud)
*注意:我不喜欢存储库是如何设置的,但它是在我进入项目之前完成的,所以我们现在必须坚持使用这种模式.
我这样称呼这个方法
public Control GetByIDWithDetail(int controlID)
{
return DataRepository.Instance.GetById<Control>(controlID, new List<string>() {"ControlDetail"});
}
Run Code Online (Sandbox Code Playgroud)
当我调试GetByID方法并查看retObj时,我可以看到已经填充了ControlDetails列表(虽然奇怪的是我还注意到没有setfetchmode设置列表正在填充)
即使使用此修复,NHProf也会使用以下行标识选择N …
楷模
class WPModel extends Eloquent
{
protected $table = 'work_processes';
protected $guarded = array('id');
protected $softDelete = true;
// declaring one-to-many relationship
public function relatedWPAQs()
{
return $this->hasMany('WPAQModel', 'wp_id');
}
public function relatedUsers()
{
return $this->belongsTo('UserModel', 'wp_owner_id');
}
}
class UserModel extends Eloquent
{
protected $table = 'users';
protected $softDelete = true;
public function relatedWPs()
{
return $this->hasMany('WPModel', 'wp_owner_id');
}
}
class WPAQModel extends Eloquent
{
protected $table = 'wp_audit_questions';
protected $fillable = array('wp_id', 'wp_audit_question');
// declaring one-to-many relationship - …Run Code Online (Sandbox Code Playgroud) 在XAML中,DataGrid绑定到一个名为EF实体的列表Results.一列被绑定到Count的Buildings导航属性.延迟加载已关闭.所以我需要包含Buildings在查询中,以获得它的数量.这会导致性能问题,因为整个Buildings实体集合会在内存中加载.但我只需要Count它.有没有办法获得Count导航属性而不将其加载到内存中?
var resQuery =
db.BAStreets
.Include("Street.StreetType")
.Include("Area.District")
.Include("Buildings")
.Where(x => true);
Results = resQuery.ToList();
Run Code Online (Sandbox Code Playgroud)
在XAML中绑定:
<DataGridTextColumn Binding="{Binding Buildings.Count}"/>
Run Code Online (Sandbox Code Playgroud)
还有一点其他问题.我用它:.Where(x => true)将DbSet强制转换为IQueryable.看起来这是一种气味的东西.什么是标准模式?
c# xaml entity-framework eager-loading navigation-properties
在获取带有相关记录的对象数组时,我得到了使用预加载以避免N + 1的好处,但是在获取单个记录时使用预加载很重要吗?
就我而言
user has_many :addresses
user has_many :skills
user has_many :devices
user has_many :authentications
Run Code Online (Sandbox Code Playgroud)
在表演动作中,我尝试使用机架迷你分析器查看是否渴望使用急切加载
User.includes(:skills, :addresses, :devices, :authentications).find(params[:id])
Run Code Online (Sandbox Code Playgroud)
但我似乎有相同数量的sql请求。
是否有针对此类情况使用紧急加载的建议?
many-to-many ruby-on-rails has-many eager-loading ruby-on-rails-4.1
我有一个Notifications属于a的模型User.我想在选择一组通知时加载用户,并仅使用names和加载模型emails.但是,使用select方法时,查询返回null.
$notifications->load(['user' => function($query){
$query->select(["name","email"]);
}]);
Run Code Online (Sandbox Code Playgroud)
如果select()方法的参数如下所示,它给出了所有字段:
$notifications->load(['user' => function($query){
$query->select(["*"]
}]);
Run Code Online (Sandbox Code Playgroud) eager-loading ×10
eloquent ×3
laravel ×2
activerecord ×1
c# ×1
ef-core-3.1 ×1
has-many ×1
inner-join ×1
laravel-4 ×1
linq ×1
many-to-many ×1
nhibernate ×1
sql ×1
typeorm ×1
typescript ×1
xaml ×1