我有一个嵌套数据模型,我想从中获取按顶级属性分组的聚合数据。我的模型例如:
public class Scan {
public long Id {get; set;}
public int ProjectId { get; set; }
public int ScanUserId { get; set; }
public ICollection<ScanItem>? Items { get; set; }
}
public class ScanItem
{
public long Id { get; set; }
public long InventoryScanId { get; set; }
public double Qty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想按 Scan.ScanUserId 分组所有扫描,然后获取 ScanItems.Qty 的总和,例如每个用户。我的查询看起来像这样,EF 给出了以下错误:
'NavigationExpandingExpressionVisitor' 处理 LINQ 表达式 'AsQueryable((Unhandled parameter: x).Items)' 失败
from scan in Scans
.Include(x=>x.ScanUser)
.Include(x=>x.Items)
group scan …
Run Code Online (Sandbox Code Playgroud) c# entity-framework entity-framework-core asp.net-core ef-core-3.1
我正在尝试为使用我的类创建单元测试EF Core DbContext
:
public class MyContext : DbContext
{
public MyContext(DbContextOptions<MyContext> options) : base(options)
{
}
public DbSet<SomeTable> SomeTables { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
消耗该上下文的类很简单:
public class MyClass
{
public MyClass(MyContext db)
{
}
}
Run Code Online (Sandbox Code Playgroud)
所以,当我尝试创建
var fakeContext = Substitute.For<MyContext>();
Run Code Online (Sandbox Code Playgroud)
以错误结束:
Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can not instantiate proxy of class: MyContext.
Could not find a parameterless constructor.
Run Code Online (Sandbox Code Playgroud)
这是由构造函数引发的base(options)
。因此,网络方法是扩展代码:
var dbContextOptions = Substitute.For<DbContextOptions<MyContext>>();
dbContextOptions.ContextType.Returns(typeof(MyContext));
var dbContextOptionsExtension = Substitute.For<IEnumerable<IDbContextOptionsExtension>>();
dbContextOptions.Extensions.Returns(dbContextOptionsExtension);
var …
Run Code Online (Sandbox Code Playgroud) 我有一个带有数据访问层的 ASP.NET Core 项目,使用 EF Core 3.1.8 和 3.1.8 版的 cli 工具。表示层和数据访问层在不同的项目中。要在 cli 中运行 EF 命令,我打开开发人员命令提示符,导航到包含数据访问层的目录并运行命令,例如
dotnet ef migrations add MyMigrationName --startup-project ../Site/Site.csproj
Run Code Online (Sandbox Code Playgroud)
或者
dotnet ef database update --startup-project ../Site/Site.csproj
Run Code Online (Sandbox Code Playgroud)
Site
在这里只是一个占位符。实际项目名称较长,--startup-project
每次想运行命令时都提供参数不方便。有没有我可以配置的地方,以便我可以更简洁地运行命令dotnet ef database update
?
我正在使用 Entity Framework Core 迈向领域驱动设计的第一步。我有一个User
实体,在简化版本中,只有Id
和ProfilePhoto
。但是,我想将个人资料照片存储在不同的表中,这就是为什么我创建了一个包含个人资料照片的拥有类型并以这种方式配置:
用户:
public class User
{
private int id;
public int Id => this.id;
//private UserProfilePhoto userProfilePhoto;
public virtual UserProfilePhoto UserProfilePhoto { get; set; }
private User()
{
}
public static User Create(byte[] profilePhoto)
{
var user = new User();
user.UserProfilePhoto = new UserProfilePhoto(profilePhoto);
return user;
}
public void SetProfilePhoto(byte[] profilePhoto)
{
this.UserProfilePhoto = new UserProfilePhoto(profilePhoto);
}
}
Run Code Online (Sandbox Code Playgroud)
用户资料照片:
public class UserProfilePhoto
{
public byte[] ProfilePhoto { get; private set; …
Run Code Online (Sandbox Code Playgroud) 我知道这DbContext
不是线程安全的,加上DbContext
缓存数据,当多个事务尝试将自己的更改保存/提交到数据库时,它可能会导致数据不一致。因此,强烈建议按请求注入它(这里)。但是我有一种情况,只有读取操作存在(在独立的类库中)并且没有事务或创建/更新/删除操作。
我的问题是:DbContext
在这种情况下作为单例注入是否安全?