代码之家  ›  专栏  ›  技术社区  ›  Hawkzey

在类库中同时存在Dbcontext时,如何在我的存储库中引用Dbcontext?

  •  0
  • Hawkzey  · 技术社区  · 7 年前

    services.AddDbContext<CitiesFrameworkCore.Entities.CityInfoContext>(o => 
    o.UseSqlServer(@"Server=(localdb)\MSSQLLocalDB;Database=CitiesFrameworkCoreDB;Trusted_Connection=True;"));
    
    services.AddScoped<ICityInfoRepository, CitiesFrameworkCore.Repositorys.CityInfoRepository>();
    

    我的问题是我不知道如何在类库存储库中引用我的上下文(我不能使用依赖注入,因为它被注入到另一个程序集中)。

     public class CityInfoRepository : ICityInfoRepository
    {
        private CityInfoContext _context;
    
        public CityInfoRepository(CityInfoContext context)
        {
            _context = context;
        }
    
        public bool CityExists(int cityId)
        {
            return _context.Cities.Any(c => c.Id == cityId);
        }
    }
    

    我的DBContext类:

     public class CityInfoContext : DbContext
    {
        public CityInfoContext(DbContextOptions<CityInfoContext> options)
           : base(options)
        {
            Database.Migrate();
        }
    
        public DbSet<City> Cities { get; set; }
        public DbSet<PointOFInterest> PointOFInterests { get; set; }
    
    }
    

    0 回复  |  直到 7 年前
    推荐文章