apawsey / DbContextScope

A simple and flexible way to manage your Entity Framework DbContext instances
http://mehdi.me/ambient-dbcontext-in-ef6/
MIT License
21 stars 9 forks source link

DbContextCollection.cs not found #4

Open rchzh opened 6 years ago

rchzh commented 6 years ago

Locating source for 'C:\Users\Adam\Documents\GitHub\apawsey\DbContextScope\EntityFramework.DbContextScope\DbContextCollection.cs'. Checksum: SHA1 {c3 15 50 d6 95 b b7 e7 26 b6 a8 46 7c d8 79 57 2c 55 17 70}

Get this error when try to query data from dbContext.

public class ApplicationDbContext : DbContext, IDbContext
    {
        private readonly string connectionString;

        public ApplicationDbContext()
            : base()
        {
            this.connectionString =
                "Server=(localdb)\\mssqllocaldb;Database=DbContextScopeDemo;Trusted_Connection=True;MultipleActiveResultSets=true";
        }

        public DbSet<Style> Styles { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(this.connectionString);
        }
}
    public class GenericRepository<T> : IGenericRepository<T>
        where T : BaseEntity
    {
        private readonly IAmbientDbContextLocator ambientDbContextLocator;

        public GenericRepository(IAmbientDbContextLocator ambientDbContextLocator)
        {
            this.ambientDbContextLocator = ambientDbContextLocator
                                           ?? throw new ArgumentNullException(nameof(ambientDbContextLocator));
        }

        private ApplicationDbContext DbContext
        {
            get
            {
                var dbContext = this.ambientDbContextLocator.Get<ApplicationDbContext>();

                if (dbContext == null)
                    throw new InvalidOperationException(
                        $"No ambient DbContext of type {nameof(ApplicationDbContext)} found.");

                dbContext.Database.EnsureCreated();

                return dbContext;
            }
        }

        public IQueryable<T> GetAll()
        {
            return this.DbContext.Set<T>();
        }
    }

I'm using .net core and ef core.


Problem only with package. Code seems like works fine

apawsey commented 4 years ago

I'll have a look at this.