c# - Java/Spring boot/Spring Data 中实体框架中的 DataContext 类的等价物
问题描述
我想知道Java/Spring boot/Spring Data中的Entity Framework中是否有等效的DataContext类?
例如,在 Entity Framework 中,我可以确定 DataContext 类并向其添加所需的工作实体:
public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options) : base(options) { }
public DbSet<User> Users { get; set; }
public DbSet<Photo> Photos { get; set; }
}
然后使用 DataContext 对象访问数据库操作:
public class DataService : IDataService
{
private readonly DataContext _context;
public DataService(DataContext context)
{
_context = context;
}
public void Add<T>(T entity)
{
_context.Add(entity);
}
}
private readonly IDataService _dataService;
_dataService.Add<User>(user);
_dataService.Add<Photo>(photo);
解决方案
推荐阅读
- r - Keras 神经网络不适合 R
- python - 编写一个函数 accumulate_product 接受一个列表作为参数并返回列表中所有非零数字的乘积
- python - 配置 conda build 以从 conda 包输出中排除某些文件夹
- ruby-on-rails - Rails 5+、WebPacker 和 Docker 开发工作流程
- c - 制作一个用于编辑数据结构中列表的 for 循环
- python - 如何添加定义模块?
- java - 使用 jMeter 维护琐碎的 RPS 数字以进行 API 测试的问题
- reactjs - 如何使用反应js在选择字段中将颜色边框底部蓝线更改为绿绿线?
- c++ - In QAbstractItemModel::data() const, how can i add some thread safe data
- javascript - Using already created functions inside a new Javascript function