将尝试使这篇文章保持简单,而不发布每个小类结构。
这是事物的大致层次结构:
项目可以出现在多个Box中,并且Item类只是该项目的属性,而与与其关联的Box / Room / User无关。
这种事情可以很好地为用户检索数据。
_context.UserRooms
.Include(b=> b.Boxes)
.ThenInclude(i=> i.Items)
.Where (ur => ur.UserId == currentUserId);
要求是允许用户将Note
附加到Item
,这样,无论该项目出现在哪个Box中,它都可能(也可能不会)有一个注释:用户放在那里。
在这里,我很困惑在哪里以及如何添加此注释。每个项目的每个用户只能有一个注释。
我想也许是一门新课
public class ItemNote {
public int ItemId {get;set;}
public Item Item {get;set;}
public Guid UserId {get;set;
public User User {get;set;}
public string Note {get;set;}
}
public class Item {
//snip existing
public ItemNote Note {get;set;}
}
但实际上这是我所了解的。如果我将其设置为Item
的属性,则不确定如何在不迭代初始查询返回的UserRoom的情况下填充该属性,并且可能会进行数千个单独的额外DB调用或设计一个接受批量UDT的存储过程并返回结果。
是否有某种方法可以更好地构造Note的结构,或制作一个查询以仅包含该用户的Note(如果没有,则为null)而不会变成混乱?
答案 0 :(得分:0)
这是一个可以正常运行的示例控制台程序,它显示了如何设置模型并对其进行一些常见查询:
import 'dart:async';
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
class ImageUtils {
static Future<File> imageToFile({String imageName, String ext}) async {
var bytes = await rootBundle.load('assets/$imageName.$ext');
String tempPath = (await getTemporaryDirectory()).path;
File file = File('$tempPath/profile.png');
await file.writeAsBytes(
bytes.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes));
return file;
}
}
class AnotherClass {
File imagePlaceHolder;
_setPlaceHolder() async {
this.imagePlaceHolder = await ImageUtils.imageToFile(
imageName: "photo_placeholder", ext: "jpg");
}
...
Image.file(this.imagePlaceHolder),
}
要立即测试代码,只需运行此.NET Fiddle。