首页 > 解决方案 > 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);

标签: c#entity-frameworkspring-bootspring-datadatacontext

解决方案


推荐阅读